home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / misc / unix / adt20.c < prev    next >
C/C++ Source or Header  |  1994-08-10  |  78KB  |  4,120 lines

  1. #if 0
  2.  ############################################################################
  3.  ##                                                                        ##
  4.  ## This is ADT 2.0, a user friendly frontend for all Aminet FTP sites. It ##
  5.  ## can be compiled easily on any UNIX flavor and AmigaDOS. Under UNIX,    ##
  6.  ## compile it using the command                                           ##
  7.  ##                                                                        ##
  8.  ##   sh adt.c            (you may append flags, e.g. sh adt.c -DNO_FTP)   ##
  9.  ##                                                                        ##
  10.  ## See adt.c.readme for further notes                                     ##
  11.  ##                                                                        ##
  12.  ############################################################################
  13.  
  14.  libs="-lcurses -ltermcap"
  15.  for i in /usr/lib/libsocket* ; do if [ -r $i ] ; then  # Kludge for systems
  16.   libs="$libs -lsocket -lnsl" ; break                   # with bash as /bin/sh
  17.  fi ; done                                              # like NetBSD or Linux
  18.  
  19.  echo cc -O -s -o adt $* $0 $libs
  20.  exec cc -O -s -o adt $* $0 $libs
  21.  exit
  22. #endif
  23.  
  24. #include <stdio.h>
  25.  
  26. #ifdef AMIGA
  27. # define getchr getch
  28. # define makedir(x,y) mkdir(x)
  29. # define popen(x,y) stdout
  30. # define pclose(x)
  31. # define sleep(x)
  32. # define COPYCMD "copy"
  33. # define CURDIR "\"\""
  34. # define TEMPPATH "t:"
  35. # define NO_FTP
  36. # define NO_FIND_CLIENT
  37. # define _system(x) puts(x)
  38. #else
  39. # define getchr getchar
  40. # define makedir(x,y) mkdir(x,y)
  41. # define COPYCMD "cp"
  42. # define CURDIR "."
  43. # define TEMPPATH "/tmp/"
  44. #endif
  45.  
  46. #ifdef NO_FTP
  47. # define NO_BUILTIN_FTP
  48. # define NO_EXTERNAL_FTP
  49. #endif
  50.  
  51. #ifdef NO_BUILTIN_FTP
  52. # ifdef NO_FIND_CLIENT
  53. #  define NO_TCP_UTILITY
  54. # endif
  55. #else /* !NO_BUILTIN_FTP */
  56. # define NO_EXTERNAL_FTP
  57. #endif
  58.  
  59. /*---------------------------------INCLUDES---------------------------------*/
  60. #define _INCLUDE_POSIX_SOURCE
  61.  
  62. #ifndef AMIGA
  63. # include <sys/types.h>
  64.  
  65. # ifdef DEBUG
  66. #  include <stdlib.h>
  67. #  include <unistd.h>
  68. #  include <string.h>
  69. #  include <sys/param.h>
  70. #  include <sys/stat.h>
  71. # endif
  72.  
  73. # ifndef NO_EXTERNAL_FTP
  74. #  include <sys/stat.h>
  75. # endif
  76.  
  77. # ifndef NO_TCP_UTILITY
  78. #  include <sys/file.h>
  79. #  include <stdio.h>
  80. #  include <netdb.h>
  81. #  include <sys/socket.h>
  82. #  include <netinet/in.h>
  83. #  include <arpa/inet.h>
  84. # endif
  85. #endif
  86.  
  87. #ifndef NO_CURSES
  88. # include <curses.h>
  89. #endif
  90.  
  91. #include <signal.h>
  92. /*---------------------------------GLOBALS-----------------------------------*/
  93.  
  94. #define E_FILE 20
  95. #define E_DIR  10
  96. #define E_DESC 44
  97.  
  98. #define K_CTRL -'@'
  99.  
  100. struct adt_entry {
  101.   long time;
  102.   long size;
  103.   long readmesize;
  104.   short version;
  105.   char readmelen;
  106.   char tagged;
  107.   char osversion;
  108.   char status;
  109.   char file[E_FILE + 1];
  110.   char dir[E_DIR + 1];
  111.   char desc[E_DESC + 1];
  112. };
  113. #define ENTRY struct adt_entry
  114.  
  115. #define STATUS_LOCAL  0
  116. #define STATUS_REMOTE 1
  117.  
  118.  
  119. struct adt_list {
  120.   ENTRY **list;
  121.   char *(*disp) ();
  122.   int mem;
  123.   int num;
  124.   int offset;
  125.   int current;
  126.   char type;
  127.   char top;
  128.   char *sel;
  129.   char *sort;
  130.   char *cmds;
  131. };
  132. #define LIST struct adt_list
  133.  
  134.  
  135. struct adt_transfer {
  136.   char *name;
  137.   char *text;
  138.   int (*init) ();
  139. };
  140. #define TRANSFER struct adt_transfer
  141.  
  142. extern TRANSFER TransferTypes[];
  143.  
  144.  
  145. LIST AllFiles, VisibleFiles, Marked, Alternate;
  146. LIST *File = &AllFiles, *Vis = &VisibleFiles, *Mark = &Marked, *Alt = &Alternate;
  147.  
  148. int Dispmode;
  149. int Width = 80, Height = 25;
  150. int LastCall;
  151. char *NoFilesString = "No files available";
  152. char HomeDir[250];
  153. char UserName[50] = "user";
  154. char ConfigFile[200] = ".adtrc";
  155. char *FindDesc = "Performing local case insensitive substring search";
  156. char GotComplete, GotLocal, GotRecent;
  157. char Flat, Readme, Silent, Path[250], Action;
  158. char InitialSetup;
  159. char Connected;
  160. char MaskSIGINT, GotSIGINT;
  161.  
  162. int (*disp_init) ();
  163. int (*disp_cleanup) ();
  164. int (*disp_choice) ();
  165. int (*disp_mainloop) ();
  166. int (*disp_report) ();
  167. int (*disp_status) ();
  168. int (*disp_confirm) ();
  169. int (*disp_more) ();
  170. int (*disp_getchar) ();
  171. int (*disp_refresh) ();
  172. int (*disp_clear) ();
  173. char *(*disp_inputstr) ();
  174.  
  175. int (*trans_options) ();
  176. int (*trans_connect) ();
  177. int (*trans_remopen) ();
  178. int (*trans_download) ();
  179. int (*trans_disconnect) ();
  180. int (*trans_close) ();
  181.  
  182. /*----------------------------main exit point--------------------------------*/
  183. int exit_adt(err)
  184.   char *err;
  185. {
  186.   if (disp_cleanup)
  187.     disp_cleanup();
  188.  
  189.   if (trans_close)
  190.     trans_close();
  191.  
  192.   if (err)
  193.     puts(err);
  194.  
  195.   exit(err ? 20 : 0);
  196.  
  197.   return 0;
  198. }
  199.  
  200. /*--------------------------quick entry allocator----------------------------*/
  201. #define CHUNKALLOC 100
  202.  
  203. struct EntryChunk {
  204.   struct EntryChunk *next;
  205.   ENTRY chunks[CHUNKALLOC];
  206. };
  207. #define CHUNK struct EntryChunk
  208.  
  209. int InCount;
  210. CHUNK *EntryChunk, *ChunkList;
  211.  
  212. char *salloc(len)
  213.   int len;
  214. {
  215.   char *t;
  216.  
  217.   if (!(t = (char *) malloc(len)))
  218.     exit_adt("out of memory\n");
  219.  
  220.   return t;
  221. }
  222.  
  223.  
  224. ENTRY *e_alloc()
  225. {
  226.   if (!InCount) {
  227.     InCount = CHUNKALLOC;
  228.     EntryChunk = (CHUNK *) salloc(sizeof(CHUNK));
  229.     EntryChunk->next = ChunkList;
  230.     ChunkList = EntryChunk;
  231.   }
  232.   return EntryChunk->chunks + --InCount;
  233. }
  234.  
  235. int e_add(l, e)
  236.   LIST *l;
  237.   ENTRY *e;
  238. {
  239.   ENTRY **oldlist;
  240.   int i;
  241.  
  242.   if (!l->mem) {
  243.     l->mem = 100;
  244.  
  245.     l->list = (ENTRY **) salloc(l->mem * sizeof(ENTRY *));
  246.   }
  247.   if (l->num == l->mem) {
  248.     oldlist = l->list;
  249.  
  250.     l->list = (ENTRY **) salloc(2 * l->mem * sizeof(ENTRY *));
  251.  
  252.     for (i = 0; i < l->num; i++)
  253.       l->list[i] = oldlist[i];
  254.  
  255.     l->mem *= 2;
  256.     free(oldlist);
  257.   }
  258.   l->list[l->num++] = e;
  259.   return 0;
  260. }
  261.  
  262. int l_reset(l)
  263.   LIST *l;
  264. {
  265.   if (l->list)
  266.     free(l->list);
  267.   l->list = 0;
  268.   l->mem = 0;
  269.   l->num = 0;
  270.   l->current = 0;
  271.   return 0;
  272. }
  273.  
  274. int l_store()
  275. {
  276.   int i;
  277.   ENTRY **e1, **e2;
  278.  
  279.   if (Alt->list)
  280.     free(Alt->list);
  281.  
  282.   if (Vis->mem)
  283.     Alt->list = (ENTRY **) salloc(Vis->mem * sizeof(ENTRY *));
  284.  
  285.   for (i = Vis->num - 1, e1 = Vis->list, e2 = Alt->list; i >= 0; i--)
  286.     *e2++ = *e1++;
  287.  
  288.   Alt->list = Vis->list;
  289.   Alt->num = Vis->num;
  290.   Alt->mem = Vis->mem;
  291.   Alt->offset = Vis->offset;
  292.   Alt->current = Vis->current;
  293.   Alt->sel = Vis->sel;
  294.   Alt->sort = Vis->sort;
  295.  
  296.   return 0;
  297. }
  298.  
  299. int e_freeall()
  300. {
  301.   CHUNK *c, *next;
  302.  
  303.   l_reset(Alt);
  304.   l_reset(Mark);
  305.   l_reset(Vis);
  306.   l_reset(File);
  307.  
  308.   for (c = ChunkList; c; c = next) {
  309.     next = c->next;
  310.     free(c);
  311.   }
  312.   ChunkList = 0;
  313.   InCount = 0;
  314.   GotComplete = GotLocal = GotRecent = 0;
  315.  
  316.   return 0;
  317. }
  318.  
  319. /*-------------------------string funcs for portability----------------------*/
  320. int mystrlen(s)
  321.   char *s;
  322. {
  323.   int n = 0;
  324.  
  325.   while (*s++)
  326.     n++;
  327.   return n;
  328. }
  329.  
  330. int mystrncpy(s1, s2, n)
  331.   char *s1, *s2;
  332.   int n;
  333. {
  334.   while (*s2 && n--)
  335.     *s1++ = *s2++;
  336.  
  337.   *s1++ = 0;
  338.  
  339.   return 0;
  340. }
  341.  
  342. char *mystrcpy(s1, s2)
  343.   char *s1, *s2;
  344. {
  345.   char *t = s1;
  346.  
  347.   while ((*s1++ = *s2++));
  348.  
  349.   return t;
  350. }
  351.  
  352. int mystrcmp(s1, s2)
  353.   char *s1, *s2;
  354. {
  355.   while (*s1 == *s2 && *s1)
  356.     s1++, s2++;
  357.  
  358.   return *s1 - *s2;
  359. }
  360.  
  361. #define LOWER(x) (x<='Z' && x>='A' ? x-'A'+'a' : x)
  362.  
  363. int mystricmp(s1, s2)
  364.   char *s1, *s2;
  365. {
  366.   while (LOWER(*s1) == LOWER(*s2) && *s1)
  367.     s1++, s2++;
  368.  
  369.   return LOWER(*s1) - LOWER(*s2);
  370. }
  371.  
  372. int mystrncmp(s1, s2, n)
  373.   char *s1, *s2;
  374.   int n;
  375. {
  376.   while (n && *s1 && *s1 == *s2)
  377.     s1++, s2++, n--;
  378.  
  379.   return n != 0 && *s1 - *s2;
  380. }
  381.  
  382. int mystrnicmp(s1, s2, n)
  383.   char *s1, *s2;
  384.   int n;
  385. {
  386.   while (n && *s1 && LOWER(*s1) == LOWER(*s2))
  387.     s1++, s2++, n--;
  388.  
  389.   return n != 0 && LOWER(*s1) - LOWER(*s2);
  390. }
  391.  
  392. char *myfgets(buf, len, file)
  393.   char *buf;
  394.   int len;
  395.   FILE *file;
  396. {
  397.   char *t, *r;
  398.  
  399.   if ((r = fgets(buf, len, file))) {
  400.     for (t = buf; *t && *t != '\n'; t++);
  401.  
  402.     *t = 0;
  403.   }
  404.   return r;
  405. }
  406.  
  407. char *mystristr(str1, str2)
  408.   char *str1, *str2;
  409. {
  410.   char c = LOWER(*str1), l = strlen(str1);
  411.  
  412.   for (;;) {
  413.     while (*str2 && LOWER(*str2) != c)
  414.       str2++;
  415.  
  416.     if (!*str2)
  417.       break;
  418.  
  419.     if (!mystrnicmp(str1, str2, l))
  420.       return str2;
  421.  
  422.     str2++;
  423.   }
  424.  
  425.   return 0;
  426. }
  427.  
  428. long myatoi(s)
  429.   char *s;
  430. {
  431.   long r = 0;
  432.  
  433.   while (*s && *s >= '0' && *s <= '9')
  434.     r = 10 * r + *s++ - '0';
  435.  
  436.   return r;
  437. }
  438.  
  439. char rdmbuf[200];
  440.  
  441. char *
  442. get_readme(e)
  443.   ENTRY *e;
  444. {
  445.   strcpy(rdmbuf,e->file);
  446.   strcpy(rdmbuf+strlen(rdmbuf)-e->readmelen, ".readme");
  447.   return rdmbuf;
  448. }
  449.  
  450. #define MYMIN(x,y) ((x)<(y) ? (x) : (y))
  451. #define MYMAX(x,y) ((x)>(y) ? (x) : (y))
  452.  
  453.  
  454. #ifdef AMIGA
  455.  
  456. char *glob_path (buf)
  457.   char *buf;
  458. {
  459.   return buf;
  460. }
  461.  
  462. #else
  463.  
  464. #include <pwd.h>
  465.  
  466. char *glob_path (buf)
  467.   char *buf;
  468. {
  469.   short bindex = 1;
  470.   struct passwd pw_info, *pw;
  471.   char buf2[300];
  472.  
  473.   if (buf[0] != '~')
  474.     return buf;
  475.  
  476.   pw_info.pw_dir = (char *) getenv("HOME");
  477.   if (buf[1] && buf[1] != '/') {
  478.     char username[128];
  479.  
  480.     while (buf[bindex] && buf[bindex] != '/')
  481.         ++bindex;
  482.     memcpy( username, buf+1, bindex-1);
  483.     username[bindex-1] = 0;
  484.  
  485.     if (pw = getpwnam(username))
  486.       pw_info.pw_dir = pw->pw_dir;
  487.     else
  488.       pw_info.pw_dir = "~";
  489.   }
  490.  
  491.   strcpy (buf2, pw_info.pw_dir);
  492.   strcat (buf2, buf + bindex);
  493.   strcpy (buf, buf2);
  494.  
  495.   return buf;
  496. }
  497. #endif
  498.  
  499.  
  500. /*-----------------------------options and settings--------------------------*/
  501. char *Options[] = {
  502.   "method", "ftp",
  503.   "fspsite", "ftp.wustl.edu,21",
  504.   "fspsites", "ftp.wustl.edu,21",
  505.   "mailserv", "ftp.doc.ic.ac.uk",
  506.   "timeout", "30",
  507.   "nomotd", "0",
  508.   "amotd", "0",
  509.   "lmotd", "0",
  510.   "sites", "0",
  511.   "locaminet", "",
  512.   "unpack", "",
  513.   "transfer", "",
  514.   "newest", "0",
  515.   "hide", "",
  516.   "printer", "",
  517.   "dlpath", "",
  518.   "compress", "1",
  519.   "send", "sz",
  520.   "silentdl", "0",
  521.   "readmedl", "0",
  522.   "flatdl", "1",
  523.   "ftpsite", "ftp.eunet.ch",
  524.   "ftpmaster", "ftp.cdrom.com",
  525.   "ftpsites", "\
  526. USA (MO),ftp.wustl.edu,128.252.135.4,,@\
  527. USA (TX),ftp.etsu.edu,192.43.199.20,,ftp.cdrom.com@\
  528. USA (CA),ftp.cdrom.com,192.153.46.2,,@\
  529. Scandinavia,ftp.luth.se,130.240.18.2,,@\
  530. Switzerland,ftp.eunet.ch,146.228.10.16,,ftp.doc.ic.ac.uk@\
  531. Switzerland,litamiga.epfl.ch,128.178.151.32,,ftp.doc.ic.ac.uk@\
  532. Germany,ftp.uni-paderborn.de,131.234.2.32,,ftp.doc.ic.ac.uk@\
  533. Germany,ftp.uni-erlangen.de,131.188.1.43,,ftp.doc.ic.ac.uk@\
  534. Germany,ftp.uni-bielefeld.de,129.70.4.55,,ftp.doc.ic.ac.uk@\
  535. Germany,ftp.uni-oldenburg.de,134.106.40.9,,ftp.doc.ic.ac.uk@\
  536. Germany,ftp.uni-kl.de,131.246.9.95,,ftp.doc.ic.ac.uk@\
  537. Germany,ftp.cs.tu-berlin.de,130.149.17.7,,ftp.doc.ic.ac.uk@\
  538. Germany,ftp.uni-stuttgart.de,129.96.8.13,aminet,ftp.doc.ic.ac.uk@\
  539. UK,ftp.doc.ic.ac.uk,146.169.2.1,,",
  540.   "findmethod", "",
  541.   "findsite", "",
  542.   "findsites", "\
  543. USA (MO),ftp.wustl.edu,128.252.135.4,,@\
  544. Switzerland,amiga.icu.net.ch,146.228.10.16,,@\
  545. Germany,wembley.uni-paderborn.de,,,",
  546.   0, 0
  547. };
  548.  
  549. #define OBUFLEN 2000
  550. char obuf[OBUFLEN];
  551.  
  552. char *option_get(name)
  553.   char *name;
  554. {
  555.   char **t;
  556.  
  557.   for (t = Options; t[0]; t += 2)
  558.     if (!mystrcmp(name, t[0]))
  559.       return t[1];
  560.  
  561.   return "";
  562. }
  563.  
  564. int option_getnum(name)
  565.   char *name;
  566. {
  567.   return myatoi(option_get(name));
  568. }
  569.  
  570. int option_set(name, val)
  571.   char *name, *val;
  572. {
  573.   char **t;
  574.  
  575.   for (t = Options; t[0]; t += 2)
  576.     if (!mystrcmp(name, t[0]))
  577.       mystrcpy((t[1] = salloc(strlen(val) + 1)), val);
  578.  
  579.   return 0;
  580. }
  581.  
  582. int option_setnum(name, val)
  583.   char *name;
  584.   int val;
  585. {
  586.   char buf[20];
  587.  
  588.   sprintf(buf, "%d", val);
  589.   option_set(name, buf);
  590.  
  591.   return 0;
  592. }
  593.  
  594. int option_loadfh(f)
  595.   FILE *f;
  596. {
  597.   char *t;
  598.  
  599.   fgets(obuf, OBUFLEN, f);
  600.   if (mystrncmp("#adtrc-v2", obuf, 9))
  601.     return 1;
  602.  
  603.   while (myfgets(obuf, OBUFLEN, f)) {
  604.     while (*obuf && obuf[strlen(obuf)-1]=='\\' && 
  605.            myfgets(obuf+strlen(obuf)-1, OBUFLEN-strlen(obuf)-1, f)) ;
  606.       
  607.     for (t = obuf; *t && *t != '='; t++);
  608.  
  609.     if (*t == '=') {
  610.       *t++ = 0;
  611.       option_set(obuf, t);
  612.     }
  613.   }
  614.  
  615.   return 0;
  616. }
  617.  
  618. int option_load(name)
  619.   char *name;
  620. {
  621.   FILE *f;
  622.   int r;
  623.  
  624.   if (!(f = fopen(name, "r")))
  625.     return 1;
  626.  
  627.   r = option_loadfh(f);
  628.  
  629.   fclose(f);
  630.  
  631.   return r;
  632. }
  633.  
  634. int option_save(name)
  635.   char *name;
  636. {
  637.   FILE *f;
  638.   char **t;
  639.  
  640.   if (!(f = fopen(name, "w")))
  641.     return 1;
  642.  
  643.   fprintf(f, "#adtrc-v2\n");
  644.  
  645.   for (t = Options; t[0]; t += 2)
  646.     fprintf(f, "%s=%s\n", t[0], t[1]);
  647.  
  648.   fclose(f);
  649.  
  650.   return 0;
  651. }
  652.  
  653. /*-----------------------------input file parser-----------------------------*/
  654. #define PBUFSIZE 400
  655. char parsebuf[PBUFSIZE], p_error, *p_ptr;
  656.  
  657. int reset_token()
  658. {
  659.   p_ptr = parsebuf;
  660.   p_error = 0;
  661.   return 0;
  662. }
  663.  
  664.  
  665. char *get_token()
  666. {
  667.   char *t, *s, *d;
  668.  
  669.   if (!p_ptr) {
  670.     p_error = 1;
  671.     return "";
  672.   }
  673.   for (t = s = d = p_ptr; *s && *s != '@'; s++) {
  674.     if (*s != '\r')
  675.       *d++ = *s;
  676.   }
  677.  
  678.   if (!*s)
  679.     p_ptr = 0;
  680.   else {
  681.     *s = 0;
  682.     p_ptr = s + 1;
  683.   }
  684.  
  685.   return t;
  686. }
  687.  
  688.  
  689. long p_atoi(s)
  690.   char *s;
  691. {
  692.   long r = 0;
  693.  
  694.   while (*s && *s >= '0' && *s <= '9')
  695.     r = 10 * r + *s++ - '0';
  696.  
  697.   if (*s)
  698.     p_error = 1;
  699.  
  700.   return r;
  701. }
  702.  
  703.  
  704. int read_adt_v2(fh)
  705.   FILE *fh;
  706. {
  707.   ENTRY *e;
  708.   char *t;
  709.  
  710.   do {
  711.     if (*parsebuf == '#')
  712.       if (!mystrncmp(parsebuf, "#endadt", 7))
  713.     break;
  714.       else
  715.     continue;
  716.  
  717.     e = e_alloc();
  718.  
  719.     reset_token();
  720.  
  721.     e->time = p_atoi(get_token());
  722.  
  723.     mystrncpy(e->dir, get_token(), E_DIR);
  724.  
  725.     mystrncpy(e->file, get_token(), E_FILE);
  726.  
  727.     e->size = p_atoi(get_token());
  728.  
  729.     e->readmelen = p_atoi(get_token());
  730.  
  731.     e->readmesize = p_atoi(get_token());
  732.  
  733.     e->version = -1;
  734.  
  735.     e->osversion = -1;
  736.  
  737.     e->status = *get_token()=='R' ? STATUS_REMOTE : STATUS_LOCAL;
  738.  
  739.     mystrncpy(e->desc, get_token(), E_DESC);
  740.  
  741.     for (t = e->desc; *t; t++)
  742.       if (*t == '\n')
  743.     *t = 0;
  744.  
  745.     e->tagged = 0;
  746.  
  747.     if (!(p_error))
  748.       e_add(File, e);
  749.   } while (fgets(parsebuf, PBUFSIZE, fh));
  750.  
  751.   return 0;
  752. }
  753.  
  754. /*----------------------------------utility----------------------------------*/
  755. long newesttime()
  756. {
  757.   int i;
  758.   long newest = 0;
  759.  
  760.   for (i = 0; i < File->num; i++)
  761.     if ((File->list[i]->time < time(NULL)) && (File->list[i]->time > newest))
  762.       newest = File->list[i]->time;
  763.  
  764.   return newest;
  765. }
  766.  
  767. char *temp_name(buf, sub)
  768.   char *buf;
  769.   char *sub;
  770. {
  771.   sprintf(buf, "%s%s_%s", TEMPPATH, UserName, sub);
  772.   return buf;
  773. }
  774.  
  775. struct temp_file {
  776.   FILE *fh;
  777.   char name[100];
  778.   int (*cleanup) ();
  779. };
  780. #define TEMPFILE struct temp_file
  781.  
  782. int temp_delete(tfh)
  783.   TEMPFILE *tfh;
  784. {
  785.   if (*tfh->name)
  786.     unlink(tfh->name);
  787.   return 0;
  788. }
  789.  
  790. int temp_dummy()
  791. {
  792.   return 0;
  793. }
  794.  
  795. char tclose(tfh)
  796.   TEMPFILE *tfh;
  797. {
  798.   fclose(tfh->fh);
  799.   if (tfh->cleanup)
  800.     (*tfh->cleanup) (tfh);
  801.   return 0;
  802. }
  803.  
  804. int exists(name)
  805.   char *name;
  806. {
  807.   FILE *f;
  808.  
  809.   if ((f = fopen(name, "r")))
  810.     fclose(f);
  811.   return f != 0;
  812. }
  813.  
  814. char *tackon(buf, add)
  815.   char *buf, *add;
  816. {
  817.   if (*buf && (buf[strlen(buf) - 1] != '/' && buf[strlen(buf) - 1] != ':'))
  818.     strcat(buf, "/");
  819.   strcat(buf, add);
  820.  
  821.   return buf;
  822. }
  823.  
  824. char *basename(s)
  825.   char *s;
  826. {
  827.   char *b = s;
  828.  
  829.   for (; *s; s++)
  830.     if (*s == '/' || *s == ':')
  831.       b = s + 1;
  832.  
  833.   return b;
  834. }
  835.  
  836.  
  837. #define NUMLINES 500
  838. char *Lines[NUMLINES + 1];
  839. char LineBuf[200];
  840.  
  841. char **read_file(f, title)
  842.   FILE *f;
  843.   char *title;
  844. {
  845.   int i=0;
  846.   char *t;
  847.  
  848.   if (title) {
  849.     Lines[i]= (char *) salloc( mystrlen(title) + 1 );
  850.     mystrcpy (Lines[i++], title);
  851.   }
  852.  
  853.   for ( ; i < NUMLINES; i++) {
  854.     if (!(fgets(LineBuf, 200, f)))
  855.       break;
  856.     for (t = LineBuf; *t && *t != '\n'; t++);
  857.     *t = 0;
  858.     Lines[i] = (char *) salloc(mystrlen(LineBuf) + 1);
  859.     mystrcpy(Lines[i], LineBuf);
  860.   }
  861.   Lines[i] = 0;
  862.   return Lines;
  863. }
  864.  
  865. int free_file(strings)
  866.   char **strings;
  867. {
  868.   while (*strings)
  869.     free(*strings++);
  870.   return 0;
  871. }
  872.  
  873. /*-----------------------------------sorting---------------------------------*/
  874.  
  875. int cmp_age(e1, e2)
  876.   ENTRY *e1, *e2;
  877. {
  878.   return e2->time - e1->time;
  879. }
  880.  
  881. int cmp_dir(e1, e2)
  882.   ENTRY *e1, *e2;
  883. {
  884.   int t;
  885.  
  886.   return (t = mystricmp(e1->dir, e2->dir)) ? t : mystricmp(e1->file, e2->file);
  887. }
  888.  
  889. int cmp_size(e1, e2)
  890.   ENTRY *e1, *e2;
  891. {
  892.   return e2->size - e1->size;
  893. }
  894.  
  895. int cmp_name(e1, e2)
  896.   ENTRY *e1, *e2;
  897. {
  898.   return mystricmp(e1->file, e2->file);
  899. }
  900.  
  901. int cmp_rage(e2, e1)
  902.   ENTRY *e1, *e2;
  903. {
  904.   return e2->time - e1->time;
  905. }
  906.  
  907. int cmp_rdir(e2, e1)
  908.   ENTRY *e1, *e2;
  909. {
  910.   int t;
  911.  
  912.   return (t = mystricmp(e1->dir, e2->dir)) ? t : mystricmp(e1->file, e2->file);
  913. }
  914.  
  915. int cmp_rsize(e2, e1)
  916.   ENTRY *e1, *e2;
  917. {
  918.   return e2->size - e1->size;
  919. }
  920.  
  921. int cmp_rname(e2, e1)
  922.   ENTRY *e1, *e2;
  923. {
  924.   return mystricmp(e1->file, e2->file);
  925. }
  926.  
  927. struct sort_mode {
  928.   int (*cmp) ();
  929.   char *desc;
  930. };
  931. #define SORT struct sort_mode
  932.  
  933. SORT
  934. sort_age   = { cmp_age,   "by age (newest first)" },
  935. sort_dir   = { cmp_dir,   "by dir" },
  936. sort_size  = { cmp_size,  "by size" },
  937. sort_name  = { cmp_name,  "by name" },
  938. sort_rage  = { cmp_rage,  "by age (oldest first)" },
  939. sort_rdir  = { cmp_rdir,  "reverse by dir" },
  940. sort_rsize = { cmp_rsize, "reverse by size" },
  941. sort_rname = { cmp_rname, "reverse by name" };
  942.  
  943. int quick_sort(av, n, cmp)
  944.   ENTRY **av;
  945.   int n, (*cmp) ();
  946. {
  947.   ENTRY **i, **j, *x, *t;
  948.  
  949.   if (n > 0) {
  950.     i = av;
  951.     j = av + n - 1;
  952.     x = av[n >> 1];
  953.     do {
  954.       while (cmp(*i, x) < 0)
  955.     i++;
  956.       while (cmp(x, *j) < 0)
  957.     --j;
  958.       if (i <= j)
  959.     t = *i, *i = *j, *j = t, i++, j--;
  960.  
  961.     } while (i <= j);
  962.  
  963.     if (j + 1 - av < av + n - i) {
  964.       quick_sort(av, j + 1 - av, cmp);
  965.       quick_sort(i, av + n - i, cmp);
  966.     } else {
  967.       quick_sort(i, av + n - i, cmp);
  968.       quick_sort(av, j + 1 - av, cmp);
  969.     }
  970.   }
  971.   return 0;
  972. }
  973.  
  974. int sort_list(list, sort)
  975.   LIST *list;
  976.   SORT *sort;
  977. {
  978.  
  979.   quick_sort(list->list, list->num, sort->cmp);
  980.  
  981.   list->current = list->offset = 0;
  982.  
  983.   Vis->sort = sort->desc;
  984.  
  985.   return 0;
  986. }
  987.  
  988.  
  989. /*---------------------------------visibility--------------------------------*/
  990.  
  991. int allvisible()
  992. {
  993.   int i;
  994.  
  995.   l_store();
  996.   l_reset(Vis);
  997.  
  998.   Vis->sel = "All files";
  999.   for (i = 0; i < File->num; i++)
  1000.     e_add(Vis, File->list[i]);
  1001.  
  1002.   sort_list(Vis, &sort_dir);
  1003.  
  1004.   return 0;
  1005. }
  1006.  
  1007. int invisible()
  1008. {
  1009.   ENTRY **entr = Vis->list;
  1010.   char buf[200], *b, *h = option_get("hide");
  1011.   int i, j;
  1012.  
  1013.   while (*h) {
  1014.     b = buf;
  1015.     while (*h && *h != ' ' && *h != ',')
  1016.       *b++ = *h++;
  1017.     *b = 0;
  1018.  
  1019.     while (*h && (*h == ' ' || *h == ','))
  1020.       h++;
  1021.  
  1022.     if (!*buf)
  1023.       continue;
  1024.  
  1025.     for (i = j = 0; i < Vis->num; i++)
  1026.       if (mystrncmp(buf, entr[i]->dir, b - buf))
  1027.     entr[j++] = entr[i];
  1028.     Vis->num = j;
  1029.   }
  1030.   Vis->current = 0;
  1031.  
  1032.   return 0;
  1033. }
  1034.  
  1035. int newvisible()
  1036. {
  1037.   ENTRY **entr = File->list;
  1038.   int i;
  1039.  
  1040.   l_store();
  1041.   l_reset(Vis);
  1042.   Vis->sel = "New files";
  1043.  
  1044.   for (i = 0; i < File->num; i++)
  1045.     if (entr[i]->time > LastCall)
  1046.       e_add(Vis, entr[i]);
  1047.  
  1048.   invisible();
  1049.  
  1050.   if (!Vis->num && File->num)
  1051.     NoFilesString = "No new files since your last call, use v)iew k)nown to see older ones";
  1052.  
  1053.   sort_list(Vis, &sort_dir);
  1054.  
  1055.   return 0;
  1056. }
  1057.  
  1058. int dirvisible(dir)
  1059.   char *dir;
  1060. {
  1061.   ENTRY **entr = File->list;
  1062.   int i, l = strlen(dir);
  1063.  
  1064.   l_store();
  1065.   l_reset(Vis);
  1066.   Vis->sel = "Selected dir";
  1067.  
  1068.   for (i = 0; i < File->num; i++)
  1069.     if (!mystrncmp(entr[i]->dir, dir, l))
  1070.       e_add(Vis, entr[i]);
  1071.  
  1072.   if (!Vis->num && File->num)
  1073.     NoFilesString = "No files matching that directory specification";
  1074.  
  1075.   sort_list(Vis, &sort_dir);
  1076.  
  1077.   return 0;
  1078. }
  1079.  
  1080. int findstr(str)
  1081.   char *str;
  1082. {
  1083.   ENTRY **entr = File->list;
  1084.   int i;
  1085.  
  1086.   if (!*str)
  1087.     return 0;
  1088.  
  1089.   l_store();
  1090.   l_reset(Vis);
  1091.  
  1092.   for (i = 0; i < File->num; i++)
  1093.     if (mystristr(str, entr[i]->desc) || mystristr(str, entr[i]->file))
  1094.       e_add(Vis, entr[i]);
  1095.  
  1096.   Vis->sel = "Matching files";
  1097.  
  1098.   sort_list(Vis, &sort_dir);
  1099.  
  1100.   if (!Vis->num && File->num)
  1101.     NoFilesString = "No files matching substring, use f)ind to search again";
  1102.  
  1103.   return 0;
  1104. }
  1105.  
  1106.  
  1107. int strlimit(str)
  1108.   char *str;
  1109. {
  1110.   ENTRY **entr = Vis->list;
  1111.   int i, j;
  1112.  
  1113.   if (!*str)
  1114.     return 0;
  1115.  
  1116.   l_store();
  1117.   Vis->sel = "Matching files";
  1118.  
  1119.   for (i = j = 0; i < Vis->num; i++)
  1120.     if (mystristr(str, entr[i]->desc) || mystristr(str, entr[i]->file))
  1121.       entr[j++] = entr[i];
  1122.  
  1123.   Vis->num = j;
  1124.   Vis->current = 0;
  1125.  
  1126.   if (!Vis->num && File->num)
  1127.     NoFilesString = "No files matching substring, use v)iew to see more";
  1128.  
  1129.   return 0;
  1130. }
  1131.  
  1132. int togglevisible()
  1133. {
  1134.   LIST *l;
  1135.  
  1136.   l = Vis;
  1137.   Vis = Alt;
  1138.   Alt = l;
  1139.   return 0;
  1140. }
  1141.  
  1142.  
  1143. int markedvisible()
  1144. {
  1145.   ENTRY **entr;
  1146.   int i, j=0;
  1147.  
  1148.   l_store();
  1149.   Vis->sel = "Marked files";
  1150.  
  1151.   entr = Vis->list;
  1152.   for (i = 0; i < Vis->num; i++)
  1153.     if (entr[i]->tagged)
  1154.       entr[j++]=entr[i];
  1155.  
  1156.   if (!(Vis->num=j) && File->num)
  1157.     NoFilesString = "No tagged files";
  1158.  
  1159.   return 0;
  1160. }
  1161.  
  1162. int markedextract()
  1163. {
  1164.   ENTRY **entr = Vis->list;
  1165.   int i;
  1166.  
  1167.   l_reset(Mark);
  1168.  
  1169.   for (i = 0; i < Vis->num; i++)
  1170.     if (entr[i]->tagged)
  1171.       e_add(Mark, entr[i]);
  1172.  
  1173.   if (!Mark->num)
  1174.     e_add(Mark, entr[Vis->current]);
  1175.  
  1176.   return 0;
  1177. }
  1178.  
  1179.  
  1180.  
  1181. /*-----------------------------string formatting-----------------------------*/
  1182. char *ShowFiles = "New files by dir";
  1183. int CurSelected, Window;
  1184.  
  1185. char *str_topline(buf, len, list)
  1186.   LIST *list;
  1187.   int len;
  1188.   char *buf;
  1189. {
  1190.   int i;
  1191.   char buf2[40];
  1192.  
  1193.   for (i = 0; i < len; i++)
  1194.     buf[i] = ' ';
  1195.   buf[i] = 0;
  1196.  
  1197.   sprintf(buf2, "Showing: %d/%d", list->num, File->num);
  1198.   strcpy(buf, buf2);
  1199.   buf[strlen(buf)] = ' ';
  1200.  
  1201.   sprintf(buf2, "%s %s", Vis->sel, Vis->sort);
  1202.   strcpy(buf + len / 2 - strlen(buf2) / 2, buf2);
  1203.   buf[strlen(buf)] = ' ';
  1204.  
  1205.   sprintf(buf2, "Page: %d/%d", 1 + list->offset / Window, 1 + list->num / Window);
  1206.   strcpy(buf + len - strlen(buf2), buf2);
  1207.  
  1208.   return buf;
  1209. }
  1210.  
  1211.  
  1212. char sizestr[10];
  1213.  
  1214. char *str_ksize(size)
  1215.   long size;
  1216. {
  1217.   if (size == -1) {
  1218.     sprintf(sizestr, "?");
  1219.   } else {
  1220.     size = (size + 512) / 1024;
  1221.     if (size <= 999)
  1222.       sprintf(sizestr, "%dK", size);
  1223.     else if ((size + 512) / 1024 < 10)
  1224.       sprintf(sizestr, "%d.%dM", (size + 512) / 1024, (size * 10 + 512) / 1024 % 10);
  1225.     else
  1226.       sprintf(sizestr, "%dM", (size + 512) / 1024);
  1227.   }
  1228.   return sizestr;
  1229. }
  1230.  
  1231.  
  1232. char *str_titles(buf)
  1233.   char *buf;
  1234. {
  1235.   switch (Dispmode) {
  1236.   case 0:
  1237.     strcpy(buf, " File                 Dir        Size Description\n");
  1238.     break;
  1239.   }
  1240.   return buf;
  1241. }
  1242.  
  1243.  
  1244.  
  1245. char *str_entry(e, n, max, buf, len)
  1246.   ENTRY **e;
  1247.   int n, max, len;
  1248.   char *buf;
  1249. {
  1250.   int i;
  1251.  
  1252.   if (n >= max) {
  1253.     for (i = 0; i < len; i++)
  1254.       buf[i] = ' ';
  1255.     buf[i] = 0;
  1256.     return buf;
  1257.   }
  1258.   switch (Dispmode) {
  1259.   case 0:
  1260.     sprintf(buf, "%c%-20.20s %-10.10s %4.4s%c%-80.80s",
  1261.         e[n]->tagged ? '+' : ' ',
  1262.         e[n]->file,
  1263.         e[n]->dir,
  1264.         str_ksize(e[n]->size),
  1265.         e[n]->readmesize > 100 ? '+' : ' ',
  1266.         e[n]->desc);
  1267.     buf[len - 1] = 0;
  1268.     break;
  1269.   }
  1270.  
  1271.  
  1272.   return buf;
  1273. }
  1274.  
  1275.  
  1276. char sitebuf[120];
  1277.  
  1278. int str_sitesplit(s, parts)
  1279.   char *s, **parts;
  1280. {
  1281.   char *d = sitebuf;
  1282.   int i;
  1283.  
  1284.   for (i = 0; i < 5; i++) {
  1285.     parts[i] = d;
  1286.     while (*s && *s != ',' && *s != '@')
  1287.       *d++ = *s++;
  1288.     if (*s == ',')
  1289.       s++;
  1290.     *d++ = 0;
  1291.   }
  1292.  
  1293.   return 0;
  1294. }
  1295.  
  1296. char *str_showline(entries, n, max, buf)
  1297.   char *entries, *buf;
  1298.   int n, max;
  1299. {
  1300.   char *s = entries;
  1301.   char *parts[10];
  1302.  
  1303.   if (n >= max)
  1304.     return "";
  1305.  
  1306.   for (; n > 0; n--)
  1307.     while (*s++ != '@');
  1308.  
  1309.   str_sitesplit(s, parts);
  1310.   sprintf(buf, "%-12.12s %24.24s", parts[0], parts[1]);
  1311.  
  1312.   return buf;
  1313. }
  1314.  
  1315. char centerbuf[250];
  1316.  
  1317. char *str_center(s, len)
  1318.   char *s;
  1319.   int len;
  1320. {
  1321.   int i = (len - strlen(s)) / 2 - 1;
  1322.  
  1323.   strcpy(centerbuf + i, s);
  1324.   while (i > 0)
  1325.     centerbuf[--i] = ' ';
  1326.  
  1327.   return centerbuf;
  1328. }
  1329.  
  1330.  
  1331. int str_longest(arr)
  1332.   char **arr;
  1333. {
  1334.   int l = 0;
  1335.  
  1336.   for (; *arr; arr++)
  1337.     if (strlen(*arr) > l)
  1338.       l = strlen(*arr);
  1339.   return l;
  1340. }
  1341.  
  1342. int str_shift(len, width)
  1343.   int len, width;
  1344. {
  1345.   return len < width ? (width - len) / 2 : 0;
  1346. }
  1347.  
  1348. LIST sitelist = {0, str_showline, 0, 0, 0, 0, 1, 0};
  1349.  
  1350. int setup_sitelist(sitesname, sitename, help)
  1351.   char *sitesname, *sitename, **help;
  1352. {
  1353.   int t, n = 1;
  1354.   char *sites = option_get(sitesname), *s;
  1355.   char buf[100], *d = buf;
  1356.  
  1357.   mystrcpy(buf, option_get("ftpsite"));
  1358.  
  1359.   for (s = sites; *s; ) {
  1360.     if (*s++ == '@') {
  1361.       if (*buf && !mystrncmp(s, buf, strlen(buf)))
  1362.     sitelist.current = n;
  1363.       n++;
  1364.     }
  1365.   }
  1366.  
  1367.   sitelist.list = (ENTRY **) sites;
  1368.   sitelist.num = n;
  1369.  
  1370.   if ((t = disp_choice(help, &sitelist)) < 0)
  1371.     return 1;
  1372.  
  1373.   for (s = sites; t > 0; t--)
  1374.     while (*s++ != '@');
  1375.  
  1376.   while (*s && *s != '@')
  1377.     *d++ = *s++;
  1378.   *d++ = 0;
  1379.  
  1380.   option_set(sitename, buf);
  1381.  
  1382.   return 0;
  1383. }
  1384.  
  1385. /*-----------------------------command key input-----------------------------*/
  1386. #define K_UP     300
  1387. #define K_DOWN   301
  1388. #define K_RIGHT  302
  1389. #define K_LEFT   303
  1390.  
  1391. int inp_getchr()
  1392. {
  1393.   int c;
  1394.  
  1395.   if ((c = disp_getchar()) != 27 && c != 155)
  1396.     return c;
  1397.  
  1398.   if (c == 27 && (c = disp_getchar()) != '[')
  1399.     return c;
  1400.  
  1401.   switch (c = disp_getchar()) {
  1402.   case 'A':
  1403.     return K_UP;
  1404.   case 'B':
  1405.     return K_DOWN;
  1406.   case 'C':
  1407.     return K_RIGHT;
  1408.   case 'D':
  1409.     return K_LEFT;
  1410.   }
  1411.   return 0;
  1412. }
  1413.  
  1414. /*-------------------------------initial setup-------------------------------*/
  1415. char *setup_showline(entries, n, max)
  1416.   TRANSFER entries[];
  1417.   int n, max;
  1418. {
  1419.   return n < max ? entries[n].text : "";
  1420. }
  1421.  
  1422. LIST setuplist = {(ENTRY **) TransferTypes, setup_showline, 0, 0, 0, 0, 1, 0};
  1423.  
  1424. char *FirstSetupHelp[] = {
  1425.   "ADT initial setup",
  1426.   "It seems that you've never used ADT before. ADT lets you access files",
  1427.   "stored in an Aminet database using various methods (FTP, FSP, mail",
  1428.   "servers and local files). The version you have here may not support all",
  1429.   "of those though. Select your transfer method now.",
  1430.   "NOTE: After setup, you can press 'h' or '?' in most places to get help.",0
  1431. };
  1432.  
  1433. char *SetupHelp[] = {
  1434.   "ADT transfer method setup",
  1435.   "Please select the way ADT should use to access files on Aminet", 0
  1436. };
  1437.  
  1438. int setup_method()
  1439. {
  1440.   TRANSFER *type;
  1441.   int n;
  1442.  
  1443.   setuplist.current = 0;
  1444.   setuplist.num = 0;
  1445.   for (type = TransferTypes; type->text; type++) {
  1446.     if (!mystrcmp(type->name, option_get("method")))
  1447.       setuplist.current = setuplist.num;
  1448.     setuplist.num++;
  1449.   }
  1450.  
  1451.   n = disp_choice(InitialSetup ? FirstSetupHelp : SetupHelp, &setuplist);
  1452.  
  1453.   if (n < 0) {
  1454.     if (InitialSetup)
  1455.       exit_adt("Cannot start up without a transfer method. Goodbye.");
  1456.   } else {
  1457.     if (trans_close)
  1458.       trans_close();
  1459.     option_set("method", TransferTypes[n].name);
  1460.     TransferTypes[n].init();
  1461.     trans_options();
  1462.   }
  1463.  
  1464.   return 0;
  1465. }
  1466.  
  1467.  
  1468. char *DefaultsHelp[] = {
  1469.   "ADT initial setup",
  1470.   "It seems that you've never used ADT before. ADT lets you access files",
  1471.   "stored in ADT databases using various methods (FTP, FSP, mailservers,",
  1472.   "local files. Your sysadmin has selected a default transfer method and",
  1473.   "default site for you. They will be saved as your local configuration",
  1474.   "when you q)uit ADT. You may change them anytime using the o)ptions",
  1475.   "command.",
  1476.   "Press any key to enter ADT", 0
  1477. };
  1478.  
  1479. int setup_defaults()
  1480. {
  1481.   option_set("newest", "0");
  1482.  
  1483.   return 0;
  1484. }
  1485.  
  1486.  
  1487.  
  1488. char *enum_showline(entries, n, max, buf)
  1489.   char **entries, *buf;
  1490.   int n, max;
  1491. {
  1492.   if (n >= max)
  1493.     return "";
  1494.   else
  1495.     return entries[n];
  1496. }
  1497.  
  1498.  
  1499. LIST enumlist = {0, enum_showline, 0, 0, 0, 0, 1, 0};
  1500.  
  1501. int setup_enumed(help, choices, var)
  1502.   char **help, **choices, *var;
  1503. {
  1504.   int n, lines, t;
  1505.  
  1506.   for (lines = 0; choices[lines]; lines++);
  1507.  
  1508.   n = option_getnum(var);
  1509.   if (n < 0)
  1510.     n = 0;
  1511.   if (n >= lines)
  1512.     n = lines - 1;
  1513.  
  1514.   enumlist.list = (ENTRY **) choices;
  1515.   enumlist.num = lines;
  1516.   enumlist.current = n;
  1517.  
  1518.   t = disp_choice(help, &enumlist);
  1519.  
  1520.   if (t >= 0)
  1521.     option_setnum(var, t);
  1522.  
  1523.   return t;
  1524. }
  1525.  
  1526.  
  1527. char *FindMethodHelp[] = {
  1528.   "Find method",
  1529.   "Please choose the method to use for finding files on Aminet. You can use",
  1530.   "an archie-like find server if you have Internet access. If you don't,",
  1531.   "or if you want to make several queries at once, or if your connection to",
  1532.   "Aminet is very fast (e.g. local files), it may be advisable for ADT to",
  1533.   "download the complete file list and do the queries locally. If you only",
  1534.   "want to search what's available at your site, pick the third option.", 0
  1535. }, *FindMethodList[] = {
  1536.   "Remote server search of complete Aminet index",
  1537.   "Local search of current Aminet site",
  1538.   "Local search of complete Aminet index", 0
  1539. };
  1540.  
  1541. char *FindSiteHelp[] = {
  1542.   "Find site selection",
  1543.   "Which server should be used for finding stuff? All servers have a complete",
  1544.   "Aminet index and can thus be used for searches on any of the complete sites", 0
  1545. };
  1546.  
  1547. int setup_findmethod()
  1548. {
  1549.   if (setup_enumed(FindMethodHelp, FindMethodList, "findmethod") == 0)
  1550.     setup_sitelist("findsites", "findsite", FindSiteHelp);
  1551.  
  1552.   option_save(ConfigFile);
  1553.  
  1554.   return 0;
  1555. }
  1556.  
  1557. /* ==============================GENERIC FILE TRANSFER======================== */
  1558. int trans_init()
  1559. {
  1560.   TRANSFER *t = TransferTypes;
  1561.   char *s = option_get("method");
  1562.  
  1563.   while (t->name && mystrcmp(t->name, s))
  1564.     t++;
  1565.  
  1566.   if (!t->name)
  1567.     return 1;
  1568.  
  1569.   t->init();
  1570.  
  1571.   return 0;
  1572. }
  1573.  
  1574.  
  1575. /* ==============================LOCAL FILE PACKAGE=========================== */
  1576. int CdRom;
  1577.  
  1578. int local_connect(progress)
  1579.   int (*progress) ();
  1580. {
  1581.   Connected = 1;
  1582.   return 0;
  1583. }
  1584.  
  1585. int local_remopen(tfh, remote)
  1586.   TEMPFILE *tfh;
  1587.   char *remote;
  1588. {
  1589.   strcpy(tfh->name, option_get("locaminet"));
  1590.   tackon(tfh->name, remote);
  1591.  
  1592.   tfh->cleanup = temp_dummy;
  1593.   tfh->fh = fopen(tfh->name, "r");
  1594.  
  1595.   NoFilesString = "Could not access remote file, use o)ptions s)ite to change path";
  1596.  
  1597.   return 0;
  1598. }
  1599.  
  1600. int local_download(remote, locdir, size, progress)
  1601.   char *remote, *locdir;
  1602.   long size;
  1603.   int (*progress) ();
  1604. {
  1605.   char buf[150], buf2[100];
  1606.  
  1607.   sprintf(buf, "Copying %s", remote);
  1608.   progress(buf);
  1609.  
  1610.   strcpy(buf2, option_get("locaminet"));
  1611.   tackon(buf2, remote);
  1612.  
  1613.   sprintf(buf, "%s %s %s", COPYCMD, buf2, locdir);
  1614.   system(buf);
  1615.  
  1616.   sprintf(buf, "Copied %s", remote);
  1617.   progress(buf);
  1618.  
  1619.   return 0;
  1620. }
  1621.  
  1622. int local_disconnect()
  1623. {
  1624.   Connected = 0;
  1625.   return 0;
  1626. }
  1627.  
  1628. #ifdef AMIGA
  1629. char *LocAminetHelp[] = {
  1630.   "Use a local Aminet database",
  1631.   "Please enter the absolute path to the Aminet files up to the aminet/",
  1632.   "directory, e.g. NET:aminet", 0
  1633. };
  1634.  
  1635. char *CdRomHelp[] = {
  1636.   "Use an Aminet CDROM",
  1637.   "Please enter the absolute path to your Aminet CDROM (or any other non",
  1638.   "updated Aminet file collection), including the Aminet/ end of the path,",
  1639.   "e.g. CD0:Aminet . Note that you need at least Aminet CD 2.", 0
  1640. };
  1641.  
  1642. #else
  1643.  
  1644. char *LocAminetHelp[] = {
  1645.   "Use a local Aminet database",
  1646.   "Please enter the absolute path to the Aminet files. You must not use the",
  1647.   "tilde ~ sign. Enter the path up to the aminet dir, eg. /ftp/pub/aminet .", 0
  1648. };
  1649.  
  1650. char *CdRomHelp[] = {
  1651.   "Please enter the absolute path to your Aminet CDROM (or any other non",
  1652.   "updated Aminet file collection), including the Aminet/ end of the path,",
  1653.   "e.g. CD0:Aminet . Note that you need at least Aminet CD 2.", 0
  1654. };
  1655. #endif
  1656.  
  1657.  
  1658.  
  1659.  
  1660. int local_options()
  1661. {
  1662.   char buf[100];
  1663.  
  1664.   if (InitialSetup)
  1665.     option_set("findmethod", "1");
  1666.  
  1667.  
  1668.   mystrcpy(buf, option_get("locaminet"));
  1669.   disp_inputstr(buf, CdRom ? CdRomHelp : LocAminetHelp);
  1670.   option_set("locaminet", buf);
  1671.  
  1672.   return 0;
  1673. }
  1674.  
  1675. int local_close()
  1676. {
  1677.   return 0;
  1678. }
  1679.  
  1680.  
  1681. int local_init()
  1682. {
  1683.   trans_options = local_options;
  1684.   trans_connect = local_connect;
  1685.   trans_remopen = local_remopen;
  1686.   trans_download = local_download;
  1687.   trans_disconnect = local_disconnect;
  1688.   trans_close = local_close;
  1689.  
  1690.   if ( !strcmp ( option_get("method"), "cdrom"))
  1691.     CdRom=1;
  1692.  
  1693.   return 0;
  1694. }
  1695.  
  1696. /* ==============================EXTERNAL FTP PACKAGE========================= */
  1697. #ifndef NO_EXTERNAL_FTP
  1698.  
  1699. FILE *xFtp;
  1700.  
  1701. int xftp_connect(progress)
  1702.   int (*progress) ();
  1703. {
  1704.   char *site = option_get("ftpsite");
  1705.   char *parts[10];
  1706.   char buf[100];
  1707.  
  1708.   if (Connected)
  1709.     return 0;
  1710.   Connected = 1;
  1711.  
  1712.   str_sitesplit(site, parts);
  1713.  
  1714.   sprintf(buf, "Connecting to %s", parts[1]);
  1715.   progress(buf);
  1716.  
  1717.   fprintf(xFtp, "open %s\n", parts[1]);
  1718.   fprintf(xFtp, "user ftp -ftp\n");
  1719.   fprintf(xFtp, "cd pub/aminet\n");
  1720.   fprintf(xFtp, "bin\n");
  1721.  
  1722.   return 0;
  1723. }
  1724.  
  1725. #ifdef AMIGA
  1726. #define xftp_filesize(name) 0
  1727. #else
  1728.  
  1729. static long xftp_filesize(name)
  1730.   char *name;
  1731. {
  1732.   struct stat st;
  1733.  
  1734.   return stat(name, &st) ? -1 : st.st_size;
  1735. }
  1736. #endif
  1737.  
  1738. static int xftp_waitfile(local, size, progress, maxwait, pad, name)
  1739.   char *local;
  1740.   long size;
  1741.   int (*progress) (), maxwait;
  1742.   char *pad, *name;
  1743. {
  1744.   int got, prevgot = -1, timeout = 0;
  1745.   char buf[100];
  1746.  
  1747.   sprintf(buf, "Initiating download of %s", size < 0 ? name : local);
  1748.   progress(buf);
  1749.  
  1750.   while ((got = xftp_filesize(local)) < size || size < 0) {
  1751.  
  1752.     if (got != prevgot)
  1753.       timeout = 0;
  1754.  
  1755.     if (timeout > maxwait)
  1756.       return 1;
  1757.  
  1758.     if (++timeout > 10) {
  1759.       sprintf(buf, "Timeout %d on file %s", maxwait - timeout, local);
  1760.       progress(buf);
  1761.     }
  1762.     prevgot = got;
  1763.     if (got >= 0) {
  1764.       if (size >= 0)
  1765.     sprintf(buf, "Downloading %s, %3dK/%3dK",
  1766.         local, (got + 512) / 1024, (size + 512) / 1024);
  1767.       else
  1768.     sprintf(buf, "Downloading %s, %3dK", name, (got + 512) / 1024);
  1769.  
  1770.       progress(buf);
  1771.     }
  1772.     if (size < 0 && exists(pad))
  1773.       return 0;
  1774.  
  1775.     sleep(1);
  1776.   }
  1777.  
  1778.   return size >= 0 ? got < size : 0;
  1779. }
  1780.  
  1781. int xftp_remopen(tfh, remote, pack, progress)
  1782.   TEMPFILE *tfh;
  1783.   char *remote, *pack;
  1784.   int (*progress) ();
  1785. {
  1786.   char pad[100], buf[100], remname[100];
  1787.  
  1788.   sprintf(remname, "%s%s", remote, pack);
  1789.  
  1790.   temp_name(pad, "PAD");
  1791.   temp_name(tfh->name, "REMOTE");
  1792.   strcat(tfh->name, pack);
  1793.   tfh->cleanup = temp_delete;
  1794.  
  1795.   unlink(pad);
  1796.   unlink(tfh->name);
  1797.  
  1798.   fprintf(xFtp, "get %s %s\n", remname, tfh->name);
  1799.   fprintf(xFtp, "get info/adt/pad %s\n", pad);
  1800.   fflush(xFtp);
  1801.  
  1802.   xftp_waitfile(tfh->name, -1, progress, 30, pad, basename(remname));
  1803.  
  1804.   unlink(pad);
  1805.  
  1806.   if (*pack) {
  1807.     temp_name(tfh->name, "REMOTE");
  1808.     unlink(tfh->name);
  1809.     sprintf(buf, "%s %s", pack[2] ? "gzip -d" : "uncompress", tfh->name);
  1810.     system(buf);
  1811.   }
  1812.   tfh->fh = fopen(tfh->name, "r");
  1813.  
  1814.   return 0;
  1815. }
  1816.  
  1817.  
  1818. int xftp_download(remote, locdir, size, progress)
  1819.   char *remote, *locdir;
  1820.   int size, (*progress) ();
  1821. {
  1822.   char locname[200];
  1823.  
  1824.   strcpy(locname, locdir);
  1825.   tackon(locname, basename(remote));
  1826.  
  1827.   fprintf(xFtp, "get %s %s\n", remote, locname);
  1828.   fflush(xFtp);
  1829.  
  1830.   xftp_waitfile(locname, size, progress, 30, 0);
  1831.  
  1832.   return 0;
  1833. }
  1834.  
  1835.  
  1836. int xftp_disconnect()
  1837. {
  1838.   if (!Connected)
  1839.     return 0;
  1840.   Connected = 0;
  1841.  
  1842.   fprintf(xFtp, "close\n");
  1843.   fflush(xFtp);
  1844.   return 0;
  1845. }
  1846.  
  1847. char *XFTPHelp[] = {
  1848.   "External FTP client setup",
  1849.   "You need a site where you retrieve your files from. Pick one that is",
  1850.   "close to where you live, and later experiment around to find out which",
  1851.   "one's the best for you.", 0
  1852. };
  1853.  
  1854.  
  1855. int xftp_options()
  1856. {
  1857.   setup_sitelist("ftpsites", "ftpsite", XFTPHelp);
  1858.  
  1859.   return 0;
  1860. }
  1861.  
  1862. int xftp_close()
  1863. {
  1864.   if (xFtp) {
  1865.     fprintf(xFtp, "bye\n");
  1866.     pclose(xFtp);
  1867.     xFtp = 0;
  1868.     Connected = 0;
  1869.   }
  1870.   return 0;
  1871. }
  1872.  
  1873. int xftp_init()
  1874. {
  1875.   char buf[100];
  1876.  
  1877.   sprintf(buf, "ftp -n");
  1878.  
  1879.   if (!xFtp && !(xFtp = popen(buf, "w")))
  1880.     exit_adt("Could not start 'ftp'");
  1881.  
  1882.   trans_options = xftp_options;
  1883.   trans_connect = xftp_connect;
  1884.   trans_remopen = xftp_remopen;
  1885.   trans_download = xftp_download;
  1886.   trans_disconnect = xftp_disconnect;
  1887.   trans_close = xftp_close;
  1888.  
  1889.   return 0;
  1890. }
  1891.  
  1892. #endif
  1893.  
  1894.  
  1895. /* ==============================TCP functions========================= */
  1896. #ifndef NO_TCP_UTILITY
  1897.  
  1898. /* creates an new tcp socket and connects it to <host> on port <port>. returns
  1899.  * -1 if a failure occured. NOTE: host can either be a hostname or a
  1900.  * dot-notation of the internet adress */
  1901. int tcp_createsocket(host, port)
  1902.   char *host;
  1903.   int port;
  1904. {
  1905.   struct sockaddr_in sin;
  1906.   struct hostent *h;
  1907.   int connected, net = -1;
  1908.  
  1909.   if ((sin.sin_addr.s_addr = inet_addr(host)) == -1) {
  1910.     if (!(h = gethostbyname(host)))
  1911.       return -1;
  1912.     else {
  1913.       sin.sin_family = h->h_addrtype;
  1914.  
  1915. #ifndef NOT43
  1916.       memcpy((caddr_t) & sin.sin_addr, h->h_addr_list[0], h->h_length);
  1917. #else
  1918.       memcpy((caddr_t) & sin.sin_addr, h->h_addr, h->h_length);
  1919. #endif
  1920.     }
  1921.   } else
  1922.     sin.sin_family = AF_INET;
  1923.  
  1924.   sin.sin_port = htons(port);
  1925.  
  1926.   for (connected = 1; connected && (connected < 30); connected++) {
  1927.     if ((net = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  1928.       return -1;
  1929.  
  1930.     if (connect(net, (struct sockaddr *) & sin, sizeof(sin)) >= 0) {
  1931.       connected = 0;
  1932.       break;
  1933.     } else
  1934.       close(net);
  1935.   }
  1936.  
  1937.   if (!connected)
  1938.     return net;
  1939.   else
  1940.     return -1;
  1941. }
  1942.  
  1943. #endif
  1944.  
  1945. /* ==============================BUILTIN FTP PACKAGE========================= */
  1946. #ifndef NO_BUILTIN_FTP
  1947.  
  1948. #ifndef BFTP_BUFSIZE
  1949. # define BFTP_BUFSIZE 10000
  1950. #endif
  1951.  
  1952. FILE *bftp_io = NULL;
  1953.  
  1954. char *BFTPHelp[] =
  1955. {
  1956.   "Builtin FTP client setup",
  1957.   "You'll need a site where you retrieve your files from. Pick one that is",
  1958.   "close to where you live, and later experiment around to find out which",
  1959.   "one's the best for you.", 0
  1960. };
  1961.  
  1962. int bftp_options()
  1963. {
  1964.   return setup_sitelist("ftpsites", "ftpsite", BFTPHelp);
  1965. }
  1966.  
  1967. int bftp_disconnect()
  1968. {
  1969.   Connected = 0;
  1970.  
  1971.   if (bftp_io)
  1972.     fclose(bftp_io);
  1973.   bftp_io = NULL;
  1974.  
  1975.   return 0;
  1976. }
  1977.  
  1978. int bftp_close()
  1979. {
  1980.   return bftp_disconnect();
  1981. }
  1982.  
  1983. /* Send FTP command and wait for reply. Returns 0 on failure. */
  1984. int bftp_cmd(cmd)
  1985.   char *cmd;
  1986. {
  1987.   char buf[256];
  1988.   int result;
  1989.  
  1990.   if (!bftp_io)
  1991.     return 0;
  1992.  
  1993.   if (cmd) {
  1994.     fflush(bftp_io);
  1995.     rewind(bftp_io);
  1996.     fputs(cmd, bftp_io);
  1997.   }
  1998.   fflush(bftp_io);
  1999.   rewind(bftp_io);
  2000.   if (!fgets(buf, sizeof(buf), bftp_io))
  2001.     return 0;
  2002.  
  2003.   result = myatoi(buf);
  2004.  
  2005.   while ((buf[3] != ' ') || (myatoi(buf) != result)) {
  2006.     if (!fgets(buf, sizeof(buf), bftp_io))
  2007.       return 0;
  2008.   }
  2009.  
  2010.   return (result >= 100) && (result < 400);
  2011. }
  2012.  
  2013. int bftp_getdataconnection()
  2014. {
  2015.   struct sockaddr_in sin;
  2016.   struct hostent *hp;
  2017.   int d, i;
  2018.   unsigned char *a, *p;
  2019.   char buf[80];
  2020.  
  2021.   gethostname(buf, sizeof(buf));
  2022.   hp = gethostbyname(buf);
  2023.  
  2024.   /* Port is not set, let system choose */
  2025.   memset((char *) &sin, '\0', sizeof(sin));
  2026.   memcpy((char *) &sin.sin_addr, hp->h_addr, hp->h_length);
  2027.   sin.sin_family = hp->h_addrtype;
  2028.  
  2029.   if ((d = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
  2030.     return -1;
  2031.  
  2032.   i = sizeof(sin);
  2033.   if ((bind(d, (struct sockaddr *) & sin, i) < 0) ||
  2034.       (getsockname(d, (struct sockaddr *) & sin, &i) < 0) ||
  2035.       (listen(d, 1) < 0)) {
  2036.     close(d);
  2037.     return -1;
  2038.   }
  2039.   a = (unsigned char *) &sin.sin_addr;
  2040.   p = (unsigned char *) &sin.sin_port;
  2041.  
  2042.   sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\n", a[0], a[1], a[2], a[3], p[0], p[1]);
  2043.   if (!bftp_cmd(buf)) {
  2044.     close(d);
  2045.     return -1;
  2046.   }
  2047.   return d;
  2048. }
  2049.  
  2050. int bftp_getfile(remote, local, size, progress)
  2051.   char *remote, *local;
  2052.   int size, (*progress) ();
  2053. {
  2054.   FILE *out = NULL;
  2055.   int s = -1, d = -1, i, got = 0, result = 0;
  2056.   char *buf, *msg = NULL, tmp[80];
  2057.   struct sockaddr_in sin;
  2058.  
  2059.   if (!Connected)
  2060.     return 0;
  2061.  
  2062.   buf = salloc(BFTP_BUFSIZE);
  2063.  
  2064.   sprintf(buf, "Getting %s...", remote);
  2065.   progress(buf);
  2066.  
  2067.   if ((s = bftp_getdataconnection()) >= 0) {
  2068.     sprintf(buf, "RETR %s\n", remote);
  2069.     if (bftp_cmd(buf)) {
  2070.       i = sizeof(sin);
  2071.       if ((d = accept(s, (struct sockaddr *) & sin, &i)) >= 0) {
  2072.     if ((out = fopen(local, "w"))) {
  2073.       int len, k, start = time(NULL), t;
  2074.       float kps;
  2075.  
  2076.       while ((len = read(d, buf, BFTP_BUFSIZE)) > 0) {
  2077.         if (fwrite(buf, len, 1, out) != 1) {
  2078.           close(s);
  2079.           fclose(out);
  2080.           msg = "Error writing to local file";
  2081.           break;
  2082.         }
  2083.         got += len;
  2084.  
  2085.         if (progress) {
  2086.           k = (got + 512) / 1024;
  2087.           kps = (float)got / (float)((t = time(NULL) - start) ? t * 1024 : 1024);
  2088.           kps = (int)(kps*10.0) / 10.0;
  2089.  
  2090.           if (size > 0)
  2091.         sprintf(tmp, "Downloading %s, %4dK/%4dK (%#3.3g Kbyte/s)",
  2092.             remote, k, (size + 512) / 1024, kps);
  2093.           else
  2094.         sprintf(tmp, "Downloading %s, %4dK (%#3.3g Kbyte/s)", remote, k, kps);
  2095.  
  2096.           progress(tmp);
  2097.         }
  2098.       }
  2099.  
  2100.       if (bftp_cmd(NULL) && ((size <= 0) || (got == size)))
  2101.         result = 1;
  2102.       else {
  2103.         sprintf(buf, "Error getting file %s (got %d bytes instead of %d)", 
  2104.             remote, got, size);
  2105.         msg = buf;
  2106.       }
  2107.     } else
  2108.       msg = "Cannot create local file";
  2109.       } else
  2110.     msg = "Cannot establish data connection to ftp site";
  2111.     } else
  2112.       msg = "Cannot get file from ftp site";
  2113.   } else
  2114.     msg = "Cannot initiate data connection to ftp site";
  2115.  
  2116.   if (progress && msg) {
  2117.     sleep(1);
  2118.     progress(msg);
  2119.     sleep(2);
  2120.   }
  2121.  
  2122.   if (d >= 0)
  2123.     close(d);
  2124.   if (s >= 0)
  2125.     close(s);
  2126.   if (out)
  2127.     fclose(out);
  2128.   free(buf);
  2129.  
  2130.   return result;
  2131. }
  2132.  
  2133. int bftp_connect(progress, remote)
  2134.   int (*progress) ();
  2135.   int remote;
  2136. {
  2137.   char *parts[10], buf[100], *msg = NULL;
  2138.   struct servent *sp;
  2139.   int s, port = 21;
  2140.   char *site;
  2141.  
  2142.   if (Connected)
  2143.     return 1;
  2144.   Connected = 1;
  2145.  
  2146.   str_sitesplit(option_get("ftpsite"), parts);
  2147.  
  2148.   if (remote)
  2149.     sprintf(buf, "Connecting to %s (master site)", site=parts[*parts[4]? 4:1]);
  2150.   else
  2151.     sprintf(buf, "Connecting to %s", site=parts[1]);
  2152.  
  2153.   progress(buf);
  2154.  
  2155.   if ((sp = getservbyname("ftp", "tcp")))
  2156.     port = ntohs(sp->s_port);
  2157.  
  2158.   if ((s = tcp_createsocket(site, port)) >= 0) {
  2159.     if ((bftp_io = fdopen(s, "r+"))) {
  2160.       sprintf (buf, "CWD %s\n", *parts[3] ? parts[3] : "pub/aminet");
  2161.       if (bftp_cmd(NULL) &&
  2162.       bftp_cmd("USER ftp\n") &&
  2163.       bftp_cmd("PASS -ftp@\n") &&
  2164.       bftp_cmd( buf ) &&
  2165.       bftp_cmd("TYPE I\n"))
  2166.     return 0;
  2167.       else
  2168.     msg = "Login failed, probably user limit reached, retry later.";
  2169.     } else {
  2170.       close(s);
  2171.       msg = "Could not communicate with ftp site.";
  2172.     }
  2173.   } else
  2174.     msg = "Could not connect to ftp site.";
  2175.  
  2176.   bftp_close();
  2177.  
  2178.   if (msg) {
  2179.     sleep(1);
  2180.     progress(msg);
  2181.     sleep(2);
  2182.   }
  2183.  
  2184.   return 1;
  2185. }
  2186.  
  2187. int bftp_remopen(tfh, remote, pack, progress)
  2188.   TEMPFILE *tfh;
  2189.   char *remote, *pack;
  2190.   int (*progress) ();
  2191. {
  2192.   char buf[200];
  2193.  
  2194.   tfh->fh = NULL;
  2195.   temp_name(tfh->name, "RECENT");
  2196.   strcat(tfh->name, pack);
  2197.   tfh->cleanup = temp_delete;
  2198.  
  2199.   unlink(tfh->name);
  2200.  
  2201.   sprintf(buf, "%s%s", remote, pack);
  2202.  
  2203.   if (bftp_getfile(buf, tfh->name, 0, progress)) {
  2204.     if (*pack) {
  2205.       temp_name(tfh->name, "RECENT");
  2206.       unlink(tfh->name);
  2207.  
  2208.       progress ("Uncompressing");
  2209.       if (pack[2]) {
  2210.         sprintf(buf,"gzip -d <%s%s >%s", tfh->name, pack, tfh->name);
  2211.       } else {
  2212.         sprintf(buf,"uncompress <%s%s >%s", tfh->name, pack, tfh->name);
  2213.       }
  2214.       system(buf);
  2215.       sprintf(buf,"%s%s",tfh->name,pack);
  2216.       unlink (buf);
  2217.     }
  2218.     tfh->fh = fopen(tfh->name, "r");
  2219.   }
  2220.   NoFilesString = "Could not download index file";
  2221.  
  2222.   return 0;
  2223. }
  2224.  
  2225. int bftp_download(remote, locdir, size, progress)
  2226.   char *remote, *locdir;
  2227.   int size, (*progress) ();
  2228. {
  2229.   char buf[200];
  2230.  
  2231.   strcpy(buf, locdir);
  2232.   tackon(buf, basename(remote));
  2233.  
  2234.   bftp_getfile(remote, buf, size, progress);
  2235.  
  2236.   return 0;
  2237. }
  2238.  
  2239. int bftp_init()
  2240. {
  2241.   Connected = 0;
  2242.   trans_options = bftp_options;
  2243.   trans_connect = bftp_connect;
  2244.   trans_disconnect = bftp_disconnect;
  2245.   trans_remopen = bftp_remopen;
  2246.   trans_download = bftp_download;
  2247.   trans_close = bftp_close;
  2248.  
  2249.   return 0;
  2250. }
  2251.  
  2252. #endif
  2253.  
  2254. /* =================================FIND CLIENT=============================== */
  2255. #ifndef NO_FIND_CLIENT
  2256.  
  2257. #define FINDD_PORT 1848
  2258.  
  2259. FILE *findd_fgrep(host, string, max)
  2260.   char *host, *string;
  2261.   int max;
  2262. {
  2263.   int fd;
  2264.   char buf[200];
  2265.   int port= FINDD_PORT;
  2266.  
  2267.   sprintf(buf, "Connecting to findserver %s...", host);
  2268.   disp_clear();
  2269.   disp_report(buf);
  2270.  
  2271.   if ((fd = tcp_createsocket(host, port)) >= 0) {
  2272.     sprintf(buf, "max %d ; ADTfind %s ; quit\r\n", max, string);
  2273.     write(fd, buf, strlen(buf));
  2274.     return fdopen(fd, "r");
  2275.   } else {
  2276.     sleep(1);
  2277.     disp_report("Could not connect to find server");
  2278.     sleep(2);
  2279.   }
  2280.  
  2281.   return NULL;
  2282. }
  2283.  
  2284.  
  2285. #else
  2286.  
  2287. char *NoFinddHelp[] =
  2288. {
  2289.   "Sorry, no find daemon compiled into this version", 0
  2290. };
  2291.  
  2292. FILE *findd_fgrep(host, port, string, max)
  2293.   char *host, *string;
  2294.   int port, max;
  2295. {
  2296.   disp_confirm(NoFinddHelp, 0);
  2297.   return NULL;
  2298. }
  2299.  
  2300. #endif
  2301.  
  2302. /* =================================MAIN PROGRAM============================== */
  2303.  
  2304. int adt_connect (progress, remote)
  2305.   int (*progress) ();
  2306.   int remote;
  2307. {
  2308.   return trans_connect (progress, remote);
  2309. }
  2310.  
  2311. int adt_disconnect ()
  2312. {
  2313.   trans_disconnect ();
  2314.   return 0;
  2315. }
  2316.  
  2317. #ifdef SIGINT
  2318. void adt_breakcheck (sig)
  2319.   int sig;
  2320. {
  2321.   signal(SIGINT, adt_breakcheck);
  2322.  
  2323.   if (!MaskSIGINT || (MaskSIGINT && GotSIGINT))
  2324.     exit_adt ("Aborted");
  2325.   GotSIGINT=1;
  2326. }
  2327. #endif
  2328.  
  2329.  
  2330. int tagcurrent()
  2331. {
  2332.   if (Vis->num && Vis->list[Vis->current])
  2333.     Vis->list[Vis->current]->tagged ^= 1;
  2334.  
  2335.   return 0;
  2336. }
  2337.  
  2338. int tagall()
  2339. {
  2340.   int tag, i;
  2341.   ENTRY **e = Vis->list;
  2342.  
  2343.   if (Vis->num) {
  2344.     tag = Vis->list[0]->tagged ^ 1;
  2345.     for (i = 0; i < Vis->num; i++)
  2346.       e[i]->tagged = tag;
  2347.   }
  2348.   return 0;
  2349. }
  2350.  
  2351. char transferdesc[200];
  2352.  
  2353. char *transferstr()
  2354. {
  2355.   char *proc = Action ? (Action == 2 ? "unpack" : "send") : "none";
  2356.  
  2357.   sprintf(transferdesc,
  2358.       "Action: %-6s  Path: %-16.16s  Readmes: %c  Subdirs: %c  Verbose: %c",
  2359.       proc, Path, Readme ? 'y' : 'n', Flat ? 'n' : 'y', Silent ? 'n' : 'y');
  2360.  
  2361.   return transferdesc;
  2362. }
  2363.  
  2364. int progress_dummy()
  2365. {
  2366.   return 0;
  2367. }
  2368.  
  2369. int make_dirs(path)
  2370.   char *path;
  2371. {
  2372.   char buf[200], *s = path, *d = buf;
  2373.  
  2374.   do {
  2375.     while (*s && *s != '/')
  2376.       *d++ = *s++;
  2377.  
  2378.     *d = 0;
  2379.  
  2380.     if (access(buf, 3))
  2381.       makedir(buf, 0755);
  2382.  
  2383.     if (*s)
  2384.       *d++ = *s++;
  2385.   } while (*s);
  2386.  
  2387.   return 0;
  2388. }
  2389.  
  2390.  
  2391. int do_action(locdir, remname)
  2392.   char *locdir, *remname;
  2393. {
  2394.   char locname[200];
  2395.   char buf[200];
  2396.   int len;
  2397.  
  2398.   strcpy(locname, locdir);
  2399.   tackon(locname, basename(remname));
  2400.  
  2401.   if (Action == 1) {
  2402.     sprintf(buf, "%s %s", option_get("send"), locname);
  2403.     system(buf);
  2404.     sleep(2);
  2405.     disp_refresh();
  2406.   }
  2407.   if (Action == 2) {
  2408.     len = mystrlen(locname);
  2409.  
  2410.     if (len > 4 && !mystricmp(locname + len - 4, ".lha")) {
  2411.       puts("");
  2412.       sprintf(buf, "cd %s;lha e %s", locdir, locname);
  2413.       system(buf);
  2414.       disp_refresh();
  2415.     } else if (len > 2 && !mystrcmp(locname + len - 2, ".Z")) {
  2416.       puts("");
  2417.       sprintf(buf, "cd %s;uncompress %s", locdir, locname);
  2418.       system(buf);
  2419.       disp_refresh();
  2420.     } else if (len > 3 && !mystrcmp(locname + len - 3, ".gz")) {
  2421.       puts("");
  2422.       sprintf(buf, "cd %s;gzip -d %s", locdir, locname);
  2423.       system(buf);
  2424.       disp_refresh();
  2425.     }
  2426.   }
  2427.   return 0;
  2428. }
  2429.  
  2430. int get_files(progress, showfile)
  2431.   int (*progress) (), (*showfile) ();
  2432. {
  2433.   ENTRY *e;
  2434.   char locdir[200], remname[200];
  2435.   int i, p, err = 0, n;
  2436.  
  2437.   if (Silent)
  2438.     progress = progress_dummy;
  2439.  
  2440.  
  2441.   MaskSIGINT = 1;
  2442.  
  2443.   for (p=0; p<2; p++) {
  2444.  
  2445.     for (i = n = 0; i < Vis->num; i++)
  2446.       if ( (Vis->list[i]->status & 1) == p)
  2447.         n++;
  2448.  
  2449.     if (!n)
  2450.       continue;
  2451.  
  2452.     if (adt_connect(progress, p))
  2453.       continue;
  2454.  
  2455.     for (i = 0; i < Vis->num; i++) {
  2456.       e = Vis->list[i];
  2457.  
  2458.       if ((e->status & 1) != p)
  2459.         continue;
  2460.  
  2461.       showfile (Vis, i);
  2462.  
  2463.       mystrcpy(locdir, option_get("dlpath"));
  2464.       glob_path(locdir);
  2465.       if (!Flat)
  2466.         tackon(locdir, e->dir);
  2467.       make_dirs(locdir);
  2468.  
  2469.       strcpy(remname, e->dir);
  2470.       tackon(remname, e->file);
  2471.  
  2472.       if (!*locdir)
  2473.         strcpy(locdir, CURDIR);
  2474.  
  2475.       if ((err = trans_download(remname, locdir, e->size, progress)))
  2476.         break;
  2477.  
  2478.       if ( GotSIGINT )
  2479.         break;
  2480.  
  2481.       do_action(locdir, remname);
  2482.  
  2483.       if (Readme) {
  2484.         strcpy(remname, e->dir); 
  2485.         tackon(remname, get_readme(e));
  2486.  
  2487.         if ((err = trans_download(remname, locdir, e->readmesize, progress)))
  2488.       break;
  2489.  
  2490.         if ( GotSIGINT )
  2491.           break;
  2492.  
  2493.         do_action(locdir, remname);
  2494.       }
  2495.  
  2496.       if (GotSIGINT) {
  2497.         progress ("Interrupted");
  2498.         break;
  2499.       }
  2500.  
  2501.       e->tagged = 0;
  2502.       showfile (Vis, i);
  2503.  
  2504.     }
  2505.  
  2506.     adt_disconnect();
  2507.  
  2508.     if (err)
  2509.       disp_status("Error during download");
  2510.  
  2511.   }
  2512.  
  2513.   MaskSIGINT = GotSIGINT = 0;
  2514.  
  2515.   return 0;
  2516. }
  2517.  
  2518. int find_string(string, progress)
  2519.   char *string;
  2520.   int (*progress) ();
  2521. {
  2522.   FILE *fh;
  2523.   char *site;
  2524.   char *parts[10];
  2525.  
  2526.   NoFilesString = "No files found matching your pattern";
  2527.   Vis->sel = "Found files";
  2528.  
  2529.   if (!*option_get("findmethod"))
  2530.     if (setup_findmethod())
  2531.       return 1;
  2532.  
  2533.   switch (option_getnum("findmethod")) {
  2534.  
  2535.   case 0:
  2536.  
  2537.     GotComplete = GotLocal = GotRecent = 0;
  2538.  
  2539.     site = option_get("findsite");
  2540.  
  2541.     progress("Searching...");
  2542.  
  2543.     str_sitesplit(site, parts);
  2544.  
  2545.     fh = findd_fgrep(parts[1], string, 100);
  2546.  
  2547.     if (fh) {
  2548.       e_freeall();
  2549.       fgets(parsebuf, 120, fh);
  2550.       read_adt_v2(fh);
  2551.       allvisible();
  2552.       fclose(fh);
  2553.     }
  2554.     Vis->sel = "Found files";
  2555.     sort_list(Vis, &sort_dir);
  2556.  
  2557.     break;
  2558.  
  2559.   case 1:
  2560.  
  2561.     get_sitelocal();
  2562.     progress("Searching...");
  2563.     findstr(string);
  2564.     break;
  2565.  
  2566.   case 2:
  2567.  
  2568.     get_complete();
  2569.     progress("Searching...");
  2570.     findstr(string);
  2571.     break;
  2572.  
  2573.   }
  2574.  
  2575.   return 0;
  2576. }
  2577.  
  2578. int print_listing(width)
  2579.   int width;
  2580. {
  2581.   ENTRY **list = Vis->list;
  2582.   FILE *f;
  2583.   char file[300];
  2584.   int i;
  2585.   long now = time(NULL);
  2586.  
  2587.   strcpy (file, glob_path( option_get("printer")));
  2588.  
  2589.   if (!*file || !(f = fopen(file, "w")))
  2590.     return 0;
  2591.  
  2592.   if (width == 1) {
  2593.  
  2594.     fputs("|File                Dir        Size Description\n", f);
  2595.     fputs("|------------------- ---------- ---- -----------\n", f);
  2596.  
  2597.   } else {
  2598.  
  2599.     fputs("|File                Dir        Size Age  Description\n", f);
  2600.     fputs("|------------------- ---------- ---- ---  -----------\n", f);
  2601.  
  2602.   }
  2603.  
  2604.  
  2605.   for (i = 0; i < Vis->num; i++) {
  2606.  
  2607.     if (width == 1) {
  2608.  
  2609.       fprintf(f, "%-20.20s %-10.10s %4.4s%c%-42.42s\n",
  2610.           list[i]->file,
  2611.           list[i]->dir,
  2612.           str_ksize(list[i]->size),
  2613.           list[i]->readmesize > 100 ? '+' : ' ',
  2614.           list[i]->desc);
  2615.  
  2616.     } else {
  2617.  
  2618.       fprintf(f, "%-20.20s %-10.10s %4.4s %3.3d %c%-.80s\n",
  2619.           list[i]->file,
  2620.           list[i]->dir,
  2621.           str_ksize(list[i]->size),
  2622.           (now - list[i]->time) / 86400,
  2623.           list[i]->readmesize > 100 ? '+' : ' ',
  2624.           list[i]->desc);
  2625.  
  2626.     }
  2627.   }
  2628.  
  2629.   fclose(f);
  2630.  
  2631.   return 0;
  2632. }
  2633.  
  2634.  
  2635. int display_remote(name, title)
  2636.   char *name;
  2637.   char *title;
  2638. {
  2639.   TEMPFILE tfh;
  2640.   char **strings;
  2641.  
  2642.   trans_remopen(&tfh, name, "", disp_status);
  2643.  
  2644.   if (tfh.fh) {
  2645.  
  2646.     strings = read_file(tfh.fh, title);
  2647.     disp_more(strings);
  2648.     free_file(strings);
  2649.     tclose(&tfh);
  2650.  
  2651.   } else {
  2652.  
  2653.     disp_status ("Could not open remote file");
  2654.  
  2655.   }
  2656.  
  2657.   return 0;
  2658. }
  2659.  
  2660.  
  2661. /* =================================CURSES PACKAGE============================ */
  2662. #ifndef NO_CURSES
  2663.  
  2664. int debug(str)
  2665.   char *str;
  2666. {
  2667.   move(0, 0);
  2668.   addstr(str);
  2669.   refresh();
  2670.   sleep(1);
  2671.   return 0;
  2672. }
  2673.  
  2674. int cur_dispinit()
  2675. {
  2676.   initscr();
  2677.   nl();
  2678.   noecho();
  2679.   cbreak();
  2680.   return 0;
  2681. }
  2682.  
  2683. int cur_getchar()
  2684. {
  2685.   int c=getchr();
  2686. #ifdef SHOW_KEYS
  2687.   static int foo;
  2688.   char buf[20];
  2689.   sprintf(buf,"%2x",c);
  2690.   move(0, (foo+=2)%8);
  2691.   addstr(buf);
  2692. #endif
  2693.  
  2694.   return c;
  2695. }
  2696.  
  2697.  
  2698. #define Y_LISTTITLE 2
  2699. #define Y_LISTSTART 3
  2700. #define Y_LISTEND   (LINES-6)
  2701. #define Y_INFO      (LINES-5)
  2702. #define Y_COMMAND   (LINES-3)
  2703. #define Y_STATUS    (LINES-1)
  2704.  
  2705. #define X_COMMAND   9
  2706.  
  2707.  
  2708. int cur_helpblock(help)
  2709.   char **help;
  2710. {
  2711.   int i, m = str_shift(str_longest(help), COLS);
  2712.  
  2713.   clear();
  2714.  
  2715.   move(0, 0);
  2716.   addstr(str_center(help[0], COLS));
  2717.  
  2718.   for (i = 0; help[i + 1]; i++) {
  2719.     move(2 + i, m);
  2720.     addstr(help[i + 1]);
  2721.   }
  2722.  
  2723.   return i;
  2724. }
  2725.  
  2726. char *cur_getstringat(buf, x, y)
  2727.   char *buf;
  2728.   int x, y;
  2729. {
  2730.   char bak[300];
  2731.   int c, l = strlen(buf), p = l, i;
  2732.  
  2733.   mystrcpy(bak, buf);
  2734.  
  2735.   move(y, x);
  2736.   addstr(buf);
  2737.  
  2738.   for (;;) {
  2739.     move(y, x + p);
  2740.     refresh();
  2741.     switch (c = inp_getchr()) {
  2742.  
  2743.     case 1:
  2744.       p = 0;
  2745.       break;
  2746.  
  2747.     case 5:
  2748.       p = l;
  2749.       break;
  2750.  
  2751.     case 8: case 127:
  2752.       if (p == 0)
  2753.     break;
  2754.  
  2755.       for (i = --p; i < l; i++)
  2756.     buf[i] = buf[i + 1];
  2757.       buf[--l] = ' ';
  2758.  
  2759.       move(y, x + p);
  2760.       addstr(buf + p);
  2761.       buf[l] = 0;
  2762.       break;
  2763.  
  2764.     case 13: case 10:
  2765.       goto done;
  2766.  
  2767.     case K_RIGHT:
  2768.       if (p < l)
  2769.     p++;
  2770.       break;
  2771.  
  2772.     case K_LEFT:
  2773.       if (p > 0)
  2774.     p--;
  2775.       break;
  2776.  
  2777.     default:
  2778.       if (c < ' ' || c > 127 || l > COLS - x - 1)
  2779.     break;
  2780.  
  2781.       for (i = l; i > p; i--)
  2782.     buf[i + 1] = buf[i];
  2783.       buf[++l] = 0;
  2784.       buf[p] = c;
  2785.  
  2786.       move(y, x + p);
  2787.       addstr(buf + p);
  2788.       p++;
  2789.       break;
  2790.     }
  2791.  
  2792.   }
  2793.  
  2794. done:
  2795.   return buf;
  2796.  
  2797. }
  2798.  
  2799. int OptLen;
  2800.  
  2801. char *cur_inputstr(buf, help)
  2802.   char *buf, **help;
  2803. {
  2804.   int x = str_shift(str_longest(help), COLS);
  2805.   int y = 3 + cur_helpblock(help);
  2806.  
  2807.   move(y, x);
  2808.   addstr("> ");
  2809.  
  2810.   return cur_getstringat(buf, x + 2, y);
  2811. }
  2812.  
  2813.  
  2814. char *cur_getstring(buf)
  2815.   char *buf;
  2816. {
  2817.   int y = Y_COMMAND, x = X_COMMAND + OptLen;
  2818.   char *ret;
  2819.  
  2820.   move(y, x);
  2821.   addstr(": ");
  2822.   x += 2;
  2823.   OptLen += 2;
  2824.  
  2825.   ret = cur_getstringat(buf, x, y);
  2826.  
  2827.   OptLen = x + strlen(buf) + 1;
  2828.  
  2829.   return ret;
  2830. }
  2831.  
  2832. char dbuf[300];
  2833.  
  2834. int cur_displist(list)
  2835.   LIST *list;
  2836. {
  2837.   int i;
  2838.   char *line;
  2839.   char *t = NULL, c = 0;
  2840.  
  2841.   Window = Y_LISTEND - list->top;
  2842.  
  2843.   if (!list->num) {
  2844.     move(LINES / 2, 0);
  2845.     addstr(str_center(NoFilesString, COLS - 1));
  2846.     return 0;
  2847.   }
  2848.  
  2849.   list->offset = list->current / Window * Window;
  2850.  
  2851.   for (i = 0; i < Window; i++) {
  2852.     line = list->disp(list->list, list->offset + i, list->num, dbuf, Width);
  2853.  
  2854.     if (*line)
  2855.       move(list->top + i, list->type ? (Width - strlen(line)) / 2 : 0);
  2856.     else {
  2857.       move(list->top + i, 0);
  2858.       clrtoeol();
  2859.     }
  2860.  
  2861.     if (list->offset + i != list->current) {
  2862.       addstr(line);
  2863.     } else {
  2864.       if (list->type == 0) {
  2865.     for (t = line + 1; *t != ' '; t++);
  2866.     c = t[1];
  2867.     t[1] = 0;
  2868.       }
  2869.       standout();
  2870.       addstr(line);
  2871.       standend();
  2872.  
  2873.       if (list->type == 0) {
  2874.     t[1] = c;
  2875.     addstr(t + 1);
  2876.       }
  2877.     }
  2878.   }
  2879.  
  2880.   return 0;
  2881. }
  2882.  
  2883. int cur_dispfiles(list)
  2884.   LIST *list;
  2885. {
  2886.   Window = Y_LISTEND - Y_LISTSTART;
  2887.  
  2888.   if (list == Vis) {
  2889.     move(0, 0);
  2890.     addstr(str_topline(dbuf, Width - 1, list));
  2891.   }
  2892.  
  2893.   cur_displist(list);
  2894.  
  2895.   return 0;
  2896. }
  2897.  
  2898.  
  2899. int cur_dispmask()
  2900. {
  2901.   Window = Y_LISTEND - Y_LISTSTART;
  2902.  
  2903.   Vis->disp = str_entry;
  2904.   Vis->top = Y_LISTSTART;
  2905.  
  2906.   clear();
  2907.   move(Y_LISTTITLE, 0);
  2908.   addstr(str_titles(dbuf, Width - 1));
  2909.  
  2910.   cur_dispfiles(Vis);
  2911.  
  2912.   move(Y_INFO, 0);
  2913.   addstr(str_center(Vis->cmds, Width - 1));
  2914.  
  2915.   move(Y_COMMAND, 0);
  2916.   addstr("Command: ");
  2917.  
  2918.   refresh();
  2919.   return 0;
  2920. }
  2921.  
  2922. int cur_mark(list, on)
  2923.   LIST *list;
  2924.   int on;
  2925. {
  2926.   char *t = NULL, *line;
  2927.  
  2928.   line = list->disp(list->list, list->current, list->num, dbuf, Width - 1);
  2929.  
  2930.   if (list->type == 0) {
  2931.     for (t = line + 1; *t != ' '; t++);
  2932.     t[1] = 0;
  2933.   }
  2934.   move(list->top + list->current - list->offset,
  2935.        list->type ? (Width - strlen(line)) / 2 : 0);
  2936.  
  2937.   if (on)
  2938.     standout();
  2939.   addstr(line);
  2940.   if (on)
  2941.     standend();
  2942.  
  2943.   if (list->type == 0)
  2944.     t[1] = ' ';
  2945.  
  2946.   return 0;
  2947. }
  2948.  
  2949. int cur_gomid(list)
  2950.   LIST *list;
  2951. {
  2952.   int left = list->num - list->offset;
  2953.  
  2954.   list->current = list->offset + (left < Window ? left : Window) / 2;
  2955.   return 0;
  2956. }
  2957.  
  2958.  
  2959. int cur_moveto(list, pos)
  2960.   LIST *list;
  2961.   int pos;
  2962. {
  2963.   if (!list->num)
  2964.     return 0;
  2965.  
  2966.   if (pos < 0)
  2967.     pos = 0;
  2968.  
  2969.   if (pos > list->num - 1)
  2970.     pos = list->num - 1;
  2971.  
  2972.   if (pos == list->current)
  2973.     return 0;
  2974.  
  2975.   cur_mark(list, 0);
  2976.   list->current = pos;
  2977.  
  2978.   if (pos / Window * Window == list->offset) {
  2979.  
  2980.     cur_mark(list, 1);
  2981.  
  2982.   } else {
  2983.  
  2984.     list->offset = pos / Window * Window;
  2985.     cur_dispfiles(list);
  2986.  
  2987.   }
  2988.  
  2989.   return 0;
  2990. }
  2991.  
  2992. int cur_listnav(list, c)
  2993.   LIST *list;
  2994.   int c;
  2995. {
  2996.   switch (c) {
  2997.   case K_DOWN:
  2998.   case K_CTRL + 'N':
  2999.   case 'j':
  3000.     cur_moveto(list, list->current + 1);
  3001.     break;
  3002.  
  3003.   case K_UP:
  3004.   case K_CTRL + 'P':
  3005.   case 'k':
  3006.     cur_moveto(list, list->current - 1);
  3007.     break;
  3008.  
  3009.   case K_RIGHT:
  3010.   case ' ':
  3011.   case K_CTRL + 'F':
  3012.     cur_moveto(list, (list->current + Window)/Window*Window);
  3013.     break;
  3014.  
  3015.   case K_LEFT:
  3016.   case K_CTRL + 'H':
  3017.   case K_CTRL + 'B':
  3018.   case 'b':
  3019.     cur_moveto(list, (list->current - Window)/Window*Window);
  3020.     break;
  3021.  
  3022.   case K_CTRL + 'I':
  3023.     cur_moveto(list, list->current + 5);
  3024.     break;
  3025.  
  3026.   case '<':
  3027.     cur_moveto(list, 0);
  3028.     break;
  3029.  
  3030.   case '>':
  3031.     cur_moveto(list, list->num - 1);
  3032.     break;
  3033.  
  3034.   case K_CTRL + 'L':
  3035.     touchwin (stdscr);
  3036.     refresh();
  3037.     break;
  3038.  
  3039.   default:
  3040.     return 0;
  3041.  
  3042.   }
  3043.  
  3044.   return 1;
  3045. }
  3046.  
  3047. int cur_choice(help, list)
  3048.   char **help;
  3049.   LIST *list;
  3050. {
  3051.   char *cmd = "Select using cursor keys, press RETURN to accept or q to cancel.";
  3052.   int c, ret = 0;
  3053.  
  3054.   list->top = 3 + cur_helpblock(help);
  3055.  
  3056.   cur_displist(list);
  3057.  
  3058.   move(Y_COMMAND, list->type ? (Width - strlen(cmd)) / 2 : 0);
  3059.   addstr(cmd);
  3060.  
  3061.   refresh();
  3062.  
  3063.   for (;;) {
  3064.     c = inp_getchr();
  3065.     if (c == 'q')
  3066.       return -1;
  3067.     if (c == 10 || c == 13) {
  3068.       ret = list->current;
  3069.       goto done;
  3070.     }
  3071.     cur_listnav(list, c);
  3072.     move(Y_COMMAND, strlen(cmd) + (list->type ? (Width - strlen(cmd)) / 2 : 0));
  3073.     refresh();
  3074.   }
  3075.  
  3076. done:
  3077.   return ret;
  3078. }
  3079.  
  3080. int cur_cmdstr(string)
  3081.   char *string;
  3082. {
  3083.   move(Y_COMMAND, X_COMMAND);
  3084.   addstr(string);
  3085.   refresh();
  3086.   OptLen = strlen(string);
  3087.  
  3088.   return 0;
  3089. }
  3090.  
  3091. int cur_subopt(string)
  3092.   char *string;
  3093. {
  3094.   char buf[200], *b = buf;
  3095.   int c, i;
  3096.  
  3097.   cur_cmdstr(string);
  3098.  
  3099.   c = inp_getchr();
  3100.  
  3101.   for (i = 0; string[i] && string[i] != ' '; i++)    /* view   */
  3102.     *b++ = string[i];
  3103.   *b++ = ' ';
  3104.  
  3105.   for (i = strlen(string) - 1; i > 0; i--)    /* a)ll   */
  3106.     if (string[i] == ')' && string[i - 1] == c)
  3107.       break;
  3108.  
  3109.   if (i > 0) {            /* all    */
  3110.     *b++ = string[++i - 2];
  3111.     while (string[i] && string[i] != ' ')
  3112.       *b++ = string[i++];
  3113.   }
  3114.   OptLen = b - buf;
  3115.  
  3116.   while (b < buf + strlen(string))    /* blanks */
  3117.     *b++ = ' ';
  3118.   *b++ = 0;
  3119.  
  3120.   move(Y_COMMAND, X_COMMAND);
  3121.   addstr(buf);
  3122.   move(Y_COMMAND, X_COMMAND + OptLen);
  3123.   refresh();
  3124.   return c;
  3125. }
  3126.  
  3127. char statusbuf[300];
  3128.  
  3129.  
  3130. int cur_statusat(string, y)
  3131.   char *string;
  3132.   int y;
  3133. {
  3134.   int i, sh;
  3135.  
  3136.   for (i = 0; i < COLS - 1; i++)
  3137.     statusbuf[i] = ' ';
  3138.   statusbuf[i] = 0;
  3139.  
  3140.   strcpy(statusbuf + (sh = str_shift(strlen(string), COLS)), string);
  3141.   statusbuf[strlen(statusbuf)] = ' ';
  3142.  
  3143.   move(y, 0);
  3144.   addstr(statusbuf);
  3145.   move(y, sh + strlen(string));
  3146.   refresh();
  3147.  
  3148.   return 0;
  3149. }
  3150.  
  3151. int cur_status(string)
  3152.   char *string;
  3153. {
  3154.   cur_statusat(string, Y_STATUS);
  3155.  
  3156.   return 0;
  3157. }
  3158.  
  3159. int cur_clear()
  3160. {
  3161.   clear();
  3162.   refresh();
  3163.   return 0;
  3164. }
  3165.  
  3166. int cur_report(string)
  3167.   char *string;
  3168. {
  3169.   cur_statusat(string, LINES / 2);
  3170.   return 0;
  3171. }
  3172.  
  3173. int cur_nosubopt()
  3174. {
  3175.   char buf[200], *b = buf;
  3176.  
  3177.   while (OptLen--)
  3178.     *b++ = ' ';
  3179.   *b++ = 0;
  3180.   move(Y_COMMAND, X_COMMAND);
  3181.   addstr(buf);
  3182.   refresh();
  3183.   cur_status("");
  3184.   return 0;
  3185. }
  3186.  
  3187. int cur_more(strings)
  3188.   char **strings;
  3189. {
  3190.   int i, c, lines, offset = 0, len = str_longest(strings);
  3191.   char buf[100], *title = *strings++;
  3192.  
  3193.   for (lines = 0; strings[lines] || lines == 0; lines++);
  3194.  
  3195.   do {
  3196.     clear();
  3197.  
  3198.     move(0, str_shift(strlen(title), len));
  3199.     standout();
  3200.     addstr(title);
  3201.     standend();
  3202.  
  3203.     for (i = 0; i < LINES - 4 && offset + i < lines; i++) {
  3204.       move(2 + i, 0);
  3205.       addstr(strings[offset + i]);
  3206.     }
  3207.  
  3208.     if (offset + i < lines)
  3209.       sprintf(buf, " --- More (%d%%), b)ack, q)uit --- ",
  3210.           (offset + i) * 100 / lines);
  3211.     else
  3212.       strcpy(buf, " --- End --- ");
  3213.  
  3214.     move(MYMIN(LINES - 1, 3 + offset + i), str_shift(strlen(buf), len));
  3215.     standout();
  3216.     addstr(buf);
  3217.     standend();
  3218.     refresh();
  3219.  
  3220.     c = inp_getchr();
  3221.     if (c == 27 || c == 'q')
  3222.       break;
  3223.     else if (c == 'b' || c == K_CTRL + 'H')
  3224.       offset = MYMAX(0, offset - (LINES - 4));
  3225.     else
  3226.       offset += LINES - 4;
  3227.  
  3228.   } while (offset < lines);
  3229.  
  3230.   return 0;
  3231. }
  3232.  
  3233.  
  3234. char *MainHelp[] = {
  3235.   "Aminet Download Tool Help",
  3236.   "Welcome to the Aminet Download Tool. You can select, inspect and download",
  3237.   "files here. The command keys available:",
  3238.   "",
  3239.   "cursor    navigate on list",
  3240.   "blank     next page (also cursor right)",
  3241.   "backspace previous page (also cursor left)",
  3242.   "tab       move down five",
  3243.   "<  >      start and end of list",
  3244.   "",
  3245.   "d)ownload receive selected file(s) after setting options",
  3246.   "f)ind     locate a file anywhere on Aminet",
  3247.   "h)elp     this page",
  3248.   "o)ptions  setup page",
  3249.   "p)rint    create a listing on disk or printer",
  3250.   "q)uit     leave, remembering this call",
  3251.   "Q)uit     leave, without remembering this call",
  3252.   "r)eadme   display the .readme of current file",
  3253.   "s)ort     change sorting of displayed list",
  3254.   "t)ag      mark/unmark current file for later download (also RETURN)",
  3255.   "T)ag      mark/unmark all files",
  3256.   "v)iew     select which files to show",
  3257.   "",
  3258.   "Press ? at any submenu to get help about the commands there.",
  3259.   0
  3260. };
  3261.  
  3262. char *ViewHelp[] = {
  3263.   "The v)iew command",
  3264.   "This command selects which files to display in ATD. The suboptions:",
  3265.   "",
  3266.   "a)ll      shows the complete list of all files on Aminet, downloading it",
  3267.   "          first if necessary. No files are hidden.",
  3268.   "d)ir      pick from the currently known files the ones from the directory",
  3269.   "          you specify.",
  3270.   "h)ide     selects one or more directory (separated by commas) that should",
  3271.   "          not be shown. This info is stored in .adtrc on exit",
  3272.   "k)nown    shows all currently known files, including those that are normally",
  3273.   "          hidden",
  3274.   "l)imit    only show the files which have the given string somewhere in their",
  3275.   "          description or file name",
  3276.   "m)arked   only shows the files that have previously been marked using the",
  3277.   "          t)ag command",
  3278.   "n)ew      shows the new files since your previous call minus the hidden ones,",
  3279.   "          downloading the list if necessary",
  3280.   "s)ite     shows the list of files available at the site you are currently",
  3281.   "          connected to",
  3282.   "t)oggle   toggle between the current list and the previously displayed one.",
  3283.   0
  3284. };
  3285.  
  3286. char *SortHelp[] = {
  3287.   "The s)ort command",
  3288.   "Using this command you can select what order the files should be shown by",
  3289.   "",
  3290.   "a)ge      sorts by age, showing the newest files on top",
  3291.   "A)ge      like a)ge but showing the oldest files on top",
  3292.   "d)ir      sorts alphabetically by directory, subsorting by name",
  3293.   "D)ir      like d)ir but reverse order",
  3294.   "n)ame     sorts alphabetically by file name",
  3295.   "N)ame     like n)ame but reverse order",
  3296.   "s)ize     sorts by file size, largest on top",
  3297.   "S)ize     like s)ize, but smallst on top",
  3298.   0
  3299. };
  3300.  
  3301. int cur_editvar(varname, string)
  3302.   char *varname, *string;
  3303. {
  3304.   char buf[100];
  3305.  
  3306.   cur_status(string);
  3307.   mystrncpy(buf, option_get(varname), 99);
  3308.   cur_getstring(buf);
  3309.   option_set(varname, buf);
  3310.   return 0;
  3311. }
  3312.  
  3313. int cur_showfile(list, i)
  3314.   LIST *list;
  3315.   int i;
  3316. {
  3317.   cur_moveto (list, i);
  3318.   cur_mark   (list, 0);
  3319.   cur_mark   (list, 1);
  3320.  
  3321.   return 0;
  3322. }
  3323.  
  3324. char *DownloadHelp[] = {
  3325.   "The d)ownload command",
  3326.   "This command lets you receive files from Aminet. The suboptions:",
  3327.   "",
  3328.   "a)ction   select what to do after the download. Possibilities are either",
  3329.   "          nothing, transfer using zmodem, or archive unpacking",
  3330.   "b)egin    actually perform the download, using the current settings",
  3331.   "          selected by the other keys",
  3332.   "p)ath     lets you enter the destination path for the files, will be",
  3333.   "          created if it does not exist",
  3334.   "q)uit     do not start download, leave all undownloaded files tagged,",
  3335.   "          and return to main files selection page",
  3336.   "r)eadme   toggle between downloading the .readme files along with the",
  3337.   "          archives and not doing so",
  3338.   "s)ubdirs  toggle between putting all files in the same directory vs",
  3339.   "          re-creating the Aminet directory structure",
  3340.   "v)erbose  toggle between quiet and verbose download, where quiet suspends",
  3341.   "          all screen output until all files have been downloaded.",
  3342.   "",
  3343.   "The current settings are displayed in the bottom line. If you want to",
  3344.   "change those settings permanently, use o)ptions d)ownload.",
  3345.   0
  3346. };
  3347.  
  3348. int cur_download()
  3349. {
  3350.   LIST *l;
  3351.   int cont = 1;
  3352.   char path[300];
  3353.  
  3354.   markedextract();
  3355.  
  3356.   l = Vis;
  3357.   Vis = Mark;
  3358.   Mark = l;
  3359.  
  3360.   Flat = option_getnum("flatdl");
  3361.   Readme = option_getnum("readmedl");
  3362.   Silent = option_getnum("silentdl");
  3363.   strcpy(Path, option_get("dlpath"));
  3364.  
  3365.   cur_dispmask();
  3366.  
  3367.   do {
  3368.     cur_status(transferstr());
  3369.     cur_cmdstr("");
  3370.  
  3371.     switch (inp_getchr()) {
  3372.     case '?':
  3373.     case 'h':
  3374.       cur_more(DownloadHelp);
  3375.       cur_dispmask();
  3376.       break;
  3377.  
  3378.     case 'p':
  3379.       cur_cmdstr("path");
  3380.       cur_editvar("dlpath", "Enter path to download files to");
  3381.       strcpy(Path, option_get("dlpath"));
  3382.       strcpy(path, Path);
  3383.       make_dirs (glob_path (path));
  3384.       cur_nosubopt();
  3385.       break;
  3386.  
  3387.     case 'b':
  3388.       cur_moveto(Vis, 0);
  3389.       get_files(cur_status, cur_showfile);
  3390.       cur_nosubopt();
  3391.       cont = 0;
  3392.       break;
  3393.  
  3394.     case 'a':
  3395.       Action = ++Action % 3;
  3396.       break;
  3397.     case 'q':
  3398.       cont = 0;
  3399.       break;
  3400.     case 'r':
  3401.       Readme = !Readme;
  3402.       break;
  3403.     case 's':
  3404.       Flat = !Flat;
  3405.       break;
  3406.     case 'v':
  3407.       Silent = !Silent;
  3408.       break;
  3409.     }
  3410.  
  3411.   } while (cont);
  3412.  
  3413.   l = Vis;
  3414.   Vis = Mark;
  3415.   Mark = l;
  3416.  
  3417.   return 0;
  3418. }
  3419.  
  3420. char *OptionsHelp[] = {
  3421.   "The o)ptions command",
  3422.   "Using the o)ptions command you can modify various settings of ADT. They",
  3423.   "will be saved in .adtrc when you leave ADT using the q)uit command, but",
  3424.   "not if you use Q)uit. The settings you can modify are:",
  3425.   "",
  3426.   "c)ompression lets you pick the compression type used for downloading",
  3427.   "             Aminet contents files.",
  3428.   "d)ownload    will pick the defaults for downloading readmes, verbose or",
  3429.   "             quiet download, download path and so on",
  3430.   "f)ind        lets you pick the method used for finding things on Aminet,",
  3431.   "             ie. download and local search vs. using a search server",
  3432.   "m)ethod      will choose the method used to transfer files, e.g. FTP",
  3433.   "             mail server or local files",
  3434.   "p)rint       lets you choose the format of the listings generated by ADT",
  3435.   "             mostly wide vs. narrow format",
  3436.   "s)ite        this lets you pick the location you want to get your files",
  3437.   "             from, for example which FTP site to use", 0
  3438. };
  3439.  
  3440.  
  3441. char *CompressOptHelp[] = {
  3442.   "Compression type",
  3443.   "Please select the compression method to use for transmitting the Aminet",
  3444.   "index files. If you have very fast access (like local files), pick no",
  3445.   "compression, otherwise pick compress, unless you have 'gzip' in your",
  3446.   "path, which compresses even better at the same speed. Note, we're only",
  3447.   "talking about decompression here, its only the choice between different",
  3448.   "versions of the index files in fact.", 0
  3449. }, *CompressList[] = {
  3450.   "  none  ",
  3451.   "compress",
  3452.   "  gzip  ", 0
  3453. };
  3454.  
  3455. char *FlatOptHelp[] = {
  3456.   "Flat or tree download",
  3457.   "Please choose whether to put all downloaded files in the same",
  3458.   "directory or to create the correct subdirectory as on Aminet ",
  3459.   "for each file downloaded", 0
  3460. }, *FlatDlList[] = {
  3461.   "subdir (create subdirs)",
  3462.   "flat   (same directory)", 0
  3463. };
  3464.  
  3465. char *ReadmeOptHelp[] = {
  3466.   "Readme download",
  3467.   "Please choose whether or not to download the .readme files",
  3468.   "along with the archives", 0
  3469. }, *ReadmeDlList[] = {
  3470.   "download archives only",
  3471.   "download .readme files too", 0
  3472. };
  3473.  
  3474. char *QuietOptHelp[] = {
  3475.   "Silent download",
  3476.   "Please choose wheter or not to perform transfers quietly. If you turn on",
  3477.   "quiet mode, no screen output happens until the download is completey over.",
  3478.   "This allows you to interrupt ADT using CTRL-Z and put it in the background",
  3479.   "using 'bg' to download quietly in the background", 0
  3480. }, *QuietDlList[] = {
  3481.   "give progress reports",
  3482.   "download quietly", 0
  3483. };
  3484.  
  3485. char *FindSitesHelp[] = {
  3486.   "Find site",
  3487.   "Here you can pick the find server to use for your queries. Feel free to",
  3488.   "them all and choose the fastest. All find servers have the complete list",
  3489.   "of all Aminet files online", 0
  3490. };
  3491.  
  3492. char FindString[120];
  3493.  
  3494. int cur_cmdkeys(c)
  3495.   int c;
  3496. {
  3497.   char buf[200];
  3498.   SORT *sorttype;
  3499.   int noredraw = 0, width = 0;
  3500.  
  3501.   buf[0] = 0;
  3502.  
  3503.   switch (c) {
  3504.  
  3505.   case 'd':
  3506.     if (Vis->num)
  3507.       cur_download();
  3508.     break;
  3509.  
  3510.   case 'f':
  3511.     cur_cmdstr("find");
  3512.     cur_status("(Use 'adt -f <keyword>' to start finds from the command line)");
  3513.     FindString[0] = 0;
  3514.     cur_getstring(FindString);
  3515.     if (*FindString)
  3516.       find_string(FindString, cur_status);
  3517.     break;
  3518.  
  3519.   case 'o':
  3520. options:
  3521.     switch (cur_subopt("options c)ompression d)ownload f)ind m)ethod s)ite")) {
  3522.     case '?':
  3523.     case 'h':
  3524.       cur_more(OptionsHelp);
  3525.       cur_dispmask();
  3526.       goto options;
  3527.  
  3528.     case 'c':
  3529.       setup_enumed(CompressOptHelp, CompressList, "compress");
  3530.       break;
  3531.  
  3532.     case 'd':
  3533.       cur_editvar("dlpath", "Enter path to download files to"),
  3534.       cur_editvar("send", "Enter command to use for sending files, eg. 'sz'");
  3535.       setup_enumed(FlatOptHelp, FlatDlList, "flatdl");
  3536.       setup_enumed(ReadmeOptHelp, ReadmeDlList, "readmedl");
  3537.       setup_enumed(QuietOptHelp, QuietDlList, "quietdl");
  3538.       break;
  3539.  
  3540.     case 'f':
  3541.       setup_findmethod();
  3542.       break;
  3543.  
  3544.     case 'm':
  3545.       trans_close();
  3546.       e_freeall();
  3547.       setup_method();
  3548.       get_recent();
  3549.       newvisible();
  3550.       NoFilesString = "Use v)iew or f)ind to get files to the display";
  3551.       break;
  3552.  
  3553.     case 's':
  3554.       if (trans_options())
  3555.         break;
  3556.       if (CdRom) {
  3557.         get_sitelocal();
  3558.         allvisible();
  3559.       } else {
  3560.         get_recent();
  3561.         newvisible();
  3562.       }
  3563.       break;
  3564.  
  3565.     default:
  3566.       noredraw = 1;
  3567.     }
  3568.     cur_nosubopt();
  3569.  
  3570.     break;
  3571.  
  3572.   case 'p':
  3573.     cur_status("Narrow format fits 80 columns, wide format format 132 columns");
  3574.     switch (cur_subopt("print n)arrow w)ide")) {
  3575.     case 'n':
  3576.       width = 1;
  3577.       break;
  3578.     case 'w':
  3579.       width = 2;
  3580.       break;
  3581.     }
  3582.     if (width) {
  3583.       cur_editvar("printer", "Enter the file to print the visible files to");
  3584.       print_listing(width);
  3585.     }
  3586.     cur_nosubopt();
  3587.     noredraw = 1;
  3588.     break;
  3589.  
  3590.   case 'q':
  3591.     cur_cmdstr("quit (and save config file)");
  3592.     option_save(ConfigFile);
  3593.     exit_adt(0);
  3594.     break;
  3595.  
  3596.   case 'Q':
  3597.     cur_cmdstr("quit (without saving config file)");
  3598.     exit_adt(0);
  3599.     break;
  3600.  
  3601.   case 'v':
  3602. view:
  3603.     switch (cur_subopt("view a)ll d)ir h)ide k)nown l)imit m)arked n)ew s)ite t)oggle")) {
  3604.     case '?':
  3605.       cur_more(ViewHelp);
  3606.       cur_dispmask();
  3607.       goto view;
  3608.  
  3609.     case 'a':
  3610.       if (!GotComplete) {
  3611.     get_complete();
  3612.     allvisible();
  3613.       }
  3614.       break;
  3615.  
  3616.     case 'd':
  3617.       cur_status("Select directory to show exclusively");
  3618.       dirvisible(cur_getstring(buf));
  3619.       cur_status("");
  3620.       break;
  3621.  
  3622.     case 'h':
  3623.       cur_editvar("hide", "Enter directories to hide permanently");
  3624.       invisible();
  3625.       break;
  3626.  
  3627.     case 'k':
  3628.       allvisible();
  3629.       break;
  3630.  
  3631.     case 'l':
  3632.       cur_status("Enter string to match in currently displayed descriptions");
  3633.       strlimit(cur_getstring(buf));
  3634.       cur_status("");
  3635.       break;
  3636.  
  3637.     case 'm':
  3638.       markedvisible();
  3639.       break;
  3640.  
  3641.     case 'n':
  3642.       get_recent();
  3643.       newvisible();
  3644.       break;
  3645.  
  3646.     case 's':
  3647.       if (!GotLocal) {
  3648.     get_sitelocal();
  3649.     cur_clear();
  3650.     cur_report("Sorting...");
  3651.     allvisible();
  3652.       }
  3653.       break;
  3654.  
  3655.     case 't':
  3656.       togglevisible();
  3657.       break;
  3658.  
  3659.     default:
  3660.       noredraw = 1;
  3661.     }
  3662.  
  3663.     cur_nosubopt();
  3664.     break;
  3665.  
  3666.   case 'r':
  3667.     if (Vis->num) {
  3668.       ENTRY *e=Vis->list[Vis->current];
  3669.  
  3670.       strcpy(buf, e->dir);
  3671.       tackon(buf, get_readme(e));
  3672.       adt_connect (disp_status, e->status & 1);
  3673.       display_remote(buf, get_readme(e));
  3674.       adt_disconnect();
  3675.     }
  3676.     break;
  3677.  
  3678.   case 's':
  3679.     sorttype = 0;
  3680. sort:
  3681.     switch (cur_subopt("sort a)ge d)ir n)ame s)ize")) {
  3682.     case '?':
  3683.       cur_more(SortHelp);
  3684.       cur_dispmask();
  3685.       goto sort;
  3686.     case 'a':
  3687.       sorttype = &sort_age;
  3688.       break;
  3689.     case 'd':
  3690.       sorttype = &sort_dir;
  3691.       break;
  3692.     case 'n':
  3693.       sorttype = &sort_name;
  3694.       break;
  3695.     case 's':
  3696.       sorttype = &sort_size;
  3697.       break;
  3698.     case 'A':
  3699.       sorttype = &sort_rage;
  3700.       break;
  3701.     case 'D':
  3702.       sorttype = &sort_rdir;
  3703.       break;
  3704.     case 'N':
  3705.       sorttype = &sort_rname;
  3706.       break;
  3707.     case 'S':
  3708.       sorttype = &sort_rsize;
  3709.       break;
  3710.     }
  3711.     if (sorttype) {
  3712.       cur_status("Sorting...");
  3713.       sort_list(Vis, sorttype);
  3714.       cur_dispfiles(Vis);
  3715.       cur_status("");
  3716.     }
  3717.     noredraw = 1;
  3718.     cur_nosubopt();
  3719.     break;
  3720.  
  3721.   case 'h':
  3722.   case '?':
  3723.     cur_more(MainHelp);
  3724.     break;
  3725.  
  3726.   case 't':
  3727.   case 13: case 10:
  3728.     noredraw = 1;
  3729.     if (!Vis->num)
  3730.       break;
  3731.     cur_mark(Vis, 0);
  3732.     tagcurrent();
  3733.     cur_mark(Vis, 1);
  3734.     cur_moveto(Vis, Vis->current + 1);
  3735.     break;
  3736.  
  3737.   case 'T':
  3738.     tagall();
  3739.     break;
  3740.  
  3741.   default:
  3742.     noredraw = 1;
  3743.  
  3744.   }
  3745.  
  3746.   if (!noredraw)
  3747.     cur_dispmask();
  3748.  
  3749.   return 0;
  3750. }
  3751.  
  3752.  
  3753.  
  3754. int cur_mainloop()
  3755. {
  3756.   int c;
  3757.  
  3758.   refresh();
  3759.   cur_dispmask();
  3760.   refresh();
  3761.  
  3762.   while ((c = inp_getchr())) {
  3763.     cur_listnav(Vis, c);
  3764.     cur_cmdkeys(c);
  3765.     move(Y_COMMAND, X_COMMAND);
  3766.     refresh();
  3767.   }
  3768.  
  3769.   return 0;
  3770. }
  3771.  
  3772. int cur_cleanup()
  3773. {
  3774.   nocbreak();
  3775.   echo();
  3776.   endwin();
  3777. #ifndef AMIGA
  3778.   puts("");
  3779. #endif
  3780.   return 0;
  3781. }
  3782.  
  3783. int cur_getcmd()
  3784. {
  3785.   return getchr();
  3786. }
  3787.  
  3788.  
  3789. int cur_confirm(strings, retry)
  3790.   char **strings;
  3791.   int retry;
  3792. {
  3793.   int i, j, m = str_shift(str_longest(strings), COLS);
  3794.  
  3795.   for (i = 0; strings[i]; i++);
  3796.  
  3797.   clear();
  3798.  
  3799.   for (j = 0; j < i; j++) {
  3800.     move((LINES - i - 2) / 2 + j, m);
  3801.     addstr(strings[j]);
  3802.   }
  3803.  
  3804.   move((LINES - i - 2) / 2 + j + 1, m + str_longest(strings) - strlen("Press any key"));
  3805.   addstr("Press any key");
  3806.  
  3807.   refresh();
  3808.   inp_getchr();
  3809.  
  3810.   return 0;
  3811. }
  3812.  
  3813. int cur_refresh()
  3814. {
  3815.   refresh();
  3816.  
  3817.   return 0;
  3818. }
  3819.  
  3820. #endif
  3821.  
  3822. /*---------------------------------main program------------------------------*/
  3823. int init_vars()
  3824. {
  3825.   char *d, *t;
  3826.   int i;
  3827.  
  3828.   mystrcpy(HomeDir, (t = (char *) getenv("HOME")) ? t : "");
  3829.  
  3830.   if (*HomeDir) {
  3831.     d = UserName;
  3832.     for (i = 0; HomeDir[i]; i++)
  3833.       if (HomeDir[i] == '/')
  3834.     d = UserName;
  3835.       else
  3836.     *d++ = HomeDir[i];
  3837.     *d++ = 0;
  3838.   }
  3839. #ifdef AMIGA
  3840.   mystrcpy(ConfigFile, "S:.adtrc");
  3841. #else
  3842.   mystrcpy(ConfigFile, HomeDir);
  3843.   tackon(ConfigFile, ".adtrc");
  3844. #endif
  3845.  
  3846.   Alt->sort = Vis->sel = "";
  3847.   Alt->sort = Vis->sort = "";
  3848.   Alt->cmds = Vis->cmds = "d)ownload f)ind h)elp o)ptions p)rint q)uit r)eadme s)ort t)ag v)iew";
  3849.  
  3850.   Mark->sel = "Files to be downloaded";
  3851.   Mark->sort = "";
  3852.   Mark->cmds = "a)ction b)egin p)ath q)uit r)eadmes s)ubdirs v)erbose";
  3853.  
  3854.   return 0;
  3855. }
  3856.  
  3857. char recentbuf[100];
  3858. char *FirstConnectHelp[] = {
  3859.   "This seems to be the first time you connect. You'll be seeing the last",
  3860.   "14 days' uploads now. Next time you connect, all the files that were",
  3861.   "uploaded since this connection will be shown.", 0
  3862. };
  3863.  
  3864. char *LongAgoHelp[] = {
  3865.   "Your last connection was more than 14 days ago, but you'll only be seeing",
  3866.   "the last 14 days' uploads.  If you want to see everything, use v)iew a)ll",
  3867.   "and filter accordingly.", 0
  3868. };
  3869.  
  3870. int get_list(title, name)
  3871.   char *title, *name;
  3872. {
  3873.   TEMPFILE tfh;
  3874.   char buf[250];
  3875.   char *packer = "";
  3876.   long amotd = 0, lmotd = 0, sites = 0;
  3877.  
  3878.   e_freeall();
  3879.  
  3880.   if (option_getnum("compress") == 1)
  3881.     packer = ".Z";
  3882.  
  3883.   if (option_getnum("compress") == 2)
  3884.     packer = ".gz";
  3885.  
  3886.   sprintf(buf, "info/adt/%s", name);
  3887.  
  3888.   disp_clear();
  3889.   disp_report(title);
  3890.  
  3891.   adt_connect(disp_report, STATUS_LOCAL);
  3892.  
  3893.   NoFilesString = "Could not download index file";
  3894.   trans_remopen(&tfh, buf, packer, disp_report);
  3895.  
  3896.   if (tfh.fh) {
  3897.  
  3898.     disp_report("Reading index");
  3899.     do {
  3900.       *parsebuf = 0;
  3901.       fgets(parsebuf, PBUFSIZE, tfh.fh);
  3902.       if (!mystrncmp(parsebuf, "#amotd=", 7))
  3903.     amotd = p_atoi(parsebuf + 7);
  3904.       if (!mystrncmp(parsebuf, "#lmotd=", 7))
  3905.     lmotd = p_atoi(parsebuf + 7);
  3906.       if (!mystrncmp(parsebuf, "#sites=", 7))
  3907.     sites = p_atoi(parsebuf + 7);
  3908.  
  3909.     } while (*parsebuf == '#');
  3910.  
  3911.     read_adt_v2(tfh.fh);
  3912.     tclose(&tfh);
  3913.  
  3914.   }
  3915.  
  3916.   if (!option_getnum("nomotd")) {
  3917.     if (amotd > option_getnum("amotd")) {
  3918.       disp_clear();
  3919.       disp_report("Retrieving new Aminet message of the day");
  3920.       display_remote("info/adt/aminet-motd","Aminet message of the day");
  3921.       option_setnum("amotd", amotd);
  3922.     }
  3923.     if (lmotd > option_getnum("lmotd")) {
  3924.       disp_clear();
  3925.       disp_report("Retrieving new local message of the day");
  3926.       display_remote("info/adt/local-motd","Mirror message of the day");
  3927.       option_setnum("lmotd", lmotd);
  3928.     }
  3929.   }
  3930.   if (sites > option_getnum("sites")) {
  3931.     disp_clear();
  3932.     disp_report("Updating mirror list");
  3933.     trans_remopen(&tfh, "info/adt/sites", "", disp_status);
  3934.     if (tfh.fh) {
  3935.       option_loadfh(tfh.fh);
  3936.       tclose(&tfh);
  3937.     }
  3938.     option_setnum("sites", sites);
  3939.   }
  3940.  
  3941.   adt_disconnect();
  3942.  
  3943.   return 0;
  3944. }
  3945.  
  3946. int get_complete()
  3947. {
  3948.   if (!GotComplete)
  3949.     get_list("Downloading the list of all Aminet files", "ADT_AMINET");
  3950.  
  3951.   GotComplete = GotLocal = GotRecent = 1;
  3952.  
  3953.   return 0;
  3954. }
  3955.  
  3956. int get_sitelocal()
  3957. {
  3958.   if (!GotLocal)
  3959.     get_list("Downloading the list of files on this site", "ADT_LOCAL");
  3960.  
  3961.   GotLocal = GotRecent = 1;
  3962.  
  3963.   return 0;
  3964. }
  3965.  
  3966.  
  3967. int get_recent()
  3968. {
  3969.   long age;
  3970.  
  3971.   if (GotRecent)
  3972.     return 0;
  3973.  
  3974.   GotRecent = 1;
  3975.  
  3976.   LastCall = myatoi(option_get("newest"));
  3977.   age = time(NULL) - LastCall;
  3978.  
  3979.   if (LastCall == 0) {
  3980.     disp_confirm(FirstConnectHelp, 0);
  3981.     get_list("Downloading the list of new files", "ADT_RECENT_14");
  3982.   } else if (age < 7 * 86400) {
  3983.     get_list("Downloading the list of new files", "ADT_RECENT_7");
  3984.   } else if (age < 14 * 86400) {
  3985.     get_list("Downloading the list of new files", "ADT_RECENT_14");
  3986.   } else {
  3987.     disp_confirm(LongAgoHelp, 0);
  3988.     get_list("Downloading the list of new files", "ADT_RECENT_14");
  3989.   }
  3990.  
  3991.   if (newesttime() > option_getnum("newest"))
  3992.     option_setnum("newest", newesttime());
  3993.  
  3994.   return 0;
  3995. }
  3996.  
  3997. int init_display()
  3998. {
  3999.   disp_init = cur_dispinit;
  4000.   disp_getchar = cur_getchar;
  4001.   disp_cleanup = cur_cleanup;
  4002.   disp_choice = cur_choice;
  4003.   disp_mainloop = cur_mainloop;
  4004.   disp_report = cur_report;
  4005.   disp_status = cur_status;
  4006.   disp_confirm = cur_confirm;
  4007.   disp_more = cur_more;
  4008.   disp_inputstr = cur_inputstr;
  4009.   disp_clear = cur_clear;
  4010.   disp_refresh = cur_refresh;
  4011.  
  4012.   disp_init();
  4013.  
  4014. #ifdef SIGINT
  4015.   signal (SIGINT, adt_breakcheck);
  4016. #endif
  4017.  
  4018.   return 0;
  4019. }
  4020.  
  4021.  
  4022. int init_config()
  4023. {
  4024.   if (option_load(ConfigFile) || trans_init()) {
  4025.  
  4026.     InitialSetup = 1;
  4027.     setup_method();
  4028.     InitialSetup = 0;
  4029.     option_save(ConfigFile);
  4030.  
  4031.   }
  4032.   return 0;
  4033. }
  4034.  
  4035. int show_usage()
  4036. {
  4037.   puts("Usage: adt               (show new files since last call)");
  4038.   puts("       adt -f pattern    (find pattern using configured method)");
  4039.   puts("       adt -l            (show files local to configured site)");
  4040.   puts("       adt -n            (don't connect)");
  4041.   exit_adt(0);
  4042.   return 0;
  4043. }
  4044.  
  4045. int parse_commandline(argc, argv)
  4046.   int argc;
  4047.   char *argv[];
  4048. {
  4049.   if (argc <= 1) {
  4050.     if (CdRom) {
  4051.       get_sitelocal();
  4052.       allvisible();
  4053.     } else {
  4054.       get_recent();
  4055.       newvisible();
  4056.     }
  4057.     return 0;
  4058.   }
  4059.   if (argv[1][0] != '-' || !argv[1][1] || argv[1][2])
  4060.     show_usage();
  4061.  
  4062.   switch (argv[1][1]) {
  4063.   case 'f':
  4064.     if (argc != 3)
  4065.       show_usage();
  4066.     find_string(argv[2], cur_report);
  4067.     break;
  4068.  
  4069.   case 'l':
  4070.     if (argc != 2)
  4071.       show_usage();
  4072.     get_sitelocal();
  4073.     newvisible();
  4074.     break;
  4075.  
  4076.   case 'n':
  4077.     if (argc != 2)
  4078.       show_usage();
  4079.     NoFilesString="Use o)ptions to connect to a service/site";
  4080.     break;
  4081.  
  4082.   default:
  4083.     show_usage();
  4084.   }
  4085.  
  4086.   return 0;
  4087. }
  4088.  
  4089.  
  4090. int main(argc, argv)
  4091.   int argc;
  4092.   char *argv[];
  4093. {
  4094.   init_vars();
  4095.   init_display();
  4096.   init_config();
  4097.  
  4098.   parse_commandline(argc, argv);
  4099.   disp_mainloop();
  4100.  
  4101.   exit_adt(0);
  4102.   return 0;
  4103. }
  4104.  
  4105. TRANSFER TransferTypes[] =
  4106. {
  4107. #ifndef NO_BUILTIN_FTP
  4108.   "bftp",   "Builtin FTP: Use internal ftp routines to download files     ", bftp_init,
  4109. #endif
  4110. #ifndef NO_EXTERNAL_FTP
  4111.   "ftp",   "External FTP: Use the 'ftp' program to download files         ", xftp_init,
  4112. #endif
  4113.   "local", "Local files : Access a local, updated Aminet file collection  ", local_init,
  4114.   "cdrom", "CD-ROM      : Access an Aminet CD-ROM or non-updated files    ", local_init,
  4115.   0, 0, 0
  4116. };
  4117.  
  4118.  
  4119. /* to delete: ftp.log, pad, recent, short */
  4120.